Skip to content
Advertisement

How do I compare the values in ‘Mono’?

Comparing the values in Mono<Integer> a and Mono<Integer> b, if the value in Mono<Integer> a is larger, I want to throw an error.

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

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

Advertisement

Answer

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();
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement