Search code examples
javaproject-reactorjava.util.concurrentreactive-streams

How merge values of Mono in loop?


I have a list of entities List<Entity1> like

public class Entity1 {
    private int id;
    ....
}

I need to iterate over this list, apply my function and get Mono<Entity2> based on each Entity1. So, I will have a List<Mono<Entity2>>.

Then I need to merge this second list to Mono<List<Entity2>>

I do not know by which way I can get List<Mono<Entity2>> and then Mono<List<Entity2>>.


Solution

  • I do not by which way I can get List<Mono<Entity2>> and then Mono<List<Entity2>>

    Once you have your List<Mono<Entity2>>, you can just do:

    Flux.concat(list).collectList();
    

    ...to obtain a Mono<List<Entity2>>. This concatenates your list of Monos into a single Flux, then collects the items in that resulting Flux back into a list.