I try to inspect an existing stream but couldn't find a special operator so far.
Flux.just(1, 2, 3)
// just inspect every element
.map(e -> {
System.out.println(e);
return e;
})
.subscribe();
I'm looking for an operator like peek()
of Java Streams or tap()
in RxJS.
As you comment, doOnNext(System.out::println)
will just print every element, which may be enough for simple use cases. (In general, doOnNext()
is used for side effects, so equivalent to tap()
in that sense.)
However, if debugging is your primary purpose, you may also want to look at log()
, which gives you more information that could be useful (such as notifying you of subscriptions, requests, etc. as well as each element, and how they tie together.) For example:
Flux.just(1, 2, 3)
.log()
.subscribe();
Prints:
[ INFO] (main) | onSubscribe([Synchronous Fuseable] FluxArray.ArraySubscription)
[ INFO] (main) | request(unbounded)
[ INFO] (main) | onNext(1)
[ INFO] (main) | onNext(2)
[ INFO] (main) | onNext(3)
[ INFO] (main) | onComplete()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With