Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when do I use Mono.flatMapIterable vs Mono.flapMapMany?

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

like image 759
James Render Avatar asked Mar 04 '23 01:03

James Render


1 Answers

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.

like image 77
bsideup Avatar answered Apr 01 '23 06:04

bsideup