How can I convert from a Flux with 1 element to a Mono?
Flux.fromArray(arrayOf(1,2,1,1,1,2)) .distinct() .take(1)
How do I make this a Mono(1)?
Instead of take(1) , you could use next() . This will transform the Flux into a valued Mono by taking the first emitted item, or an empty Mono if the Flux is empty itself.
A Flux object represents a reactive sequence of 0.. N items, while a Mono object represents a single-value-or-empty (0..1) result. This distinction carries a bit of semantic information into the type, indicating the rough cardinality of the asynchronous processing.
Flatten Stream<Flux> using Flux#fromStream() + Flux#flatMap()
The flatMapMany is a generic operator on Mono that returns a Publisher. Let's apply flatMapManyto our solution: private <T> Flux<T> monoTofluxUsingFlatMapMany(Mono<List<T>> monoList) { return monoList .flatMapMany(Flux::fromIterable) .log(); }
Instead of take(1)
, you could use next()
.
This will transform the Flux
into a valued Mono
by taking the first emitted item, or an empty Mono if the Flux is empty itself.
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