I've read that wonderful answer about the difference between map
and flatMap
.
And there is a picture that demonstrates flatmap:
And quote:
The map is for synchronous, non-blocking, one-to-one transformations while the flatMap is for asynchronous (non-blocking) One-to-Many transformations.
Based on that picture and quote I understand that flatMap
allows the creation of more (or fewer) elements than it was in the initial Flux
. But all examples I was able to find contains the same amount of element in the initial sequence after flatMap
like here:
Flux<Player> players = Flux.just("Zahid Khan", "Arif Khan", "Obaid Sheikh")
.flatMap(fullname -> Mono
.just(fullname)
.map(p -> {
String[] split = p.split("\\s");
return new Player(split[0], split[1]);
})
.subscribeOn(Scheduler.parallel()));
3 strings as input and 3 players as output:
List<Player> playerList = Arrays.asList(
new Player("Zahid", "Khan"),
new Player("Arif", "Khan"),
new Player("Obaid", "Sheikh"));
My question is:
Is there a way to modify my example to achieve 3 strings as input and 6 (for example) players as output to prove that flatmap could be one-to-many?