Search code examples
javaspring-webfluxproject-reactorreactor

Reactor WebFlux: help to understand how to work flatMap()


Please help to understand how can I use fkatMap() in my example:

Flux.just("1,2,3", "4,5,6")
                .flatMap(// to do something)
                .collect(Collectors.toList())
                .subscribe(System.out::println);

I read the documentation. I understood how to work flatMap() but I can't understand how I need to use in my example. Thanks.


Solution

  • As Kayaman already answered, you can do the following:

    Flux.just("1,2,3", "4,5,6")
            .flatMap(i -> Flux.fromIterable(Arrays.asList(i.split(","))))
            .collect(Collectors.toList())
            .subscribe(System.out::println);