Search code examples
javareactive-programmingproject-reactor

How to process list of monos one by one?


I have a list as

List<Tuple3<Object1, Mono<List<Object2>>, Mono<List<Object3>>>>

And I need to write a method with signature as

private Mono<Dto> buildDto(List<Tuple3<Object1, Mono<List<Object2>>, Mono<List<Object3>>>> tuples) {...}

How I can follow all items in tuples list one by one to obtain values of Object1, Object2, Object3 without skipping no one of them?

I tried to execute

AtomicReference<Dto> dtoAtomic = new AtomicReference<>(buildEmptyDto());
return tuples.stream()
         .map(tuple3 -> {
             return Mono.zip(tuple3.getT2(), tuple3.getT3(),
                             (object2, object3) -> {
                                 Dto dto = dtoAtomic.get();

                                 dtoAtomic.set(dto);
                                 return dto;
                             });
         })
         .collect(Collectors.toList())
         .stream()
         .findFirst()
         .orElse(Mono.just(dtoAtomic.get()))

But there is only one Mono.zip(...) could be obtained but I need to obtain all of tuples.


Solution

  • Solved by using Flux.fromIterable(tuples)