Skip to content

Webflux collectMap resulting Mono<Map<String, Mono>>

I’m quite new in the reactive world

My code looks like this:

                    .collectMap(a -> a.getName(),
                            b-> functionReturningMonoOfC(b)

The result is of type Map<String, Mono<String>> . I would like it to be of type Map<String, String>. Any ideas?


I suggest to use flatMap operator before collecting the elements into a Map

public class ReactorApp {

    record Person(String name){}

    public static Mono<String> functionReturningMono(Person person) {
        return Mono.just("Hello " +;

    public static void main(String[] args) {
        List<Person> persons = List.of(
                new Person("John"),
                new Person("Mike"),
                new Person("Stacey")

        Map<String, String> result = Flux.fromIterable(persons)
                .flatMap(person -> functionReturningMono(person)
                        .map(message -> Map.entry(, message)))
                .collectMap(Map.Entry::getKey, Map.Entry::getValue)

        System.out.println("Result : " + result);
        // Result : {Mike=HELLO MIKE, Stacey=HELLO STACEY, John=HELLO JOHN}