【问题标题】:How do I compare the values in 'Mono'?如何比较“单声道”中的值?
【发布时间】:2021-06-11 10:03:49
【问题描述】:

比较Mono<Integer> aMono<Integer> b中的值,如果Mono<Integer> a中的值更大,我想抛出一个错误。

Mono<Integer> a = getA();
Mono<integer> b = getB();

if(a > b) {
 throw new RuntimeException();
}

【问题讨论】:

  • 你的比较依据是什么? 示例:我可以通过循环遍历每个字符串的字符来比较两个字符串,逐一比较,如果有不匹配,比较两个不匹配的字符。你能用Mono&lt;Integer&gt;描述一个满足你特定要求的类似过程吗?
  • @RobertHarvey 我只是想比较MonoInteger 值的大小
  • 你指的是这个吗? codingame.com/playgrounds/929/…
  • @RobertHarvey 看起来不一样,但我用下面的答案解决了它。感谢您让我知道一个好网站。

标签: java spring-webflux reactor


【解决方案1】:
Mono<Integer> a = Mono.just(12);
Mono<Integer> b = Mono.just(10);

a.zipWith(b)
    .doOnNext(tuple -> {
        if (tuple.getT1() > tuple.getT2()) {
            throw new RuntimeException();
        }
    }).subscribe();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多