New to reactor, trying to understand Mono.flatMapMany
and Mono.flatMapIterable
as often dealing with Mono's of an Object containing a collection.
Whats the difference between:
Mono.just(List.of("one", "two", "three")).log().flatMapMany(Flux::fromIterable).subscribe(System.out::println);
Mono.just(List.of("one", "two", "three")).log().flatMapIterable(l -> l).subscribe(System.out::println);
When do I use each method?
thanks
Mono#flatMapMany
is a generic, one-to-many operator.Mono#flatMapIterable
is a special operator to "flatten" the item represented as Iterable<T>
into a reactive stream of T
.
Use Mono#flatMapIterable
where possible (mapper can return Iterable) because it is optimized, use Mono#flatMapMany
when your mapper returns a Publisher
of items.
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