Lana James
Lana James

Reputation: 67

Iterate over items from a Recator Flux in another Reactor Flux

I have my Flux flux1 and when I iterate over it, I also want to start this Flux again inside of it so I can access all of my items from flux1 while iterating over the items from flux1. Basically doing this but with Flux:

for(item in flux1){
  for(item2 in flux1){
  //xxxxx
      }
    }

Thank you

Upvotes: 0

Views: 409

Answers (1)

lkatiforis
lkatiforis

Reputation: 6255

One possible solution would be to have a nested flatmap, like this:

Flux<String> flux = Flux.just("a", "b", "c");

flux.flatMap(item -> flux.flatMap(item2 -> 
    doSomethingAsync(item, item2).doOnNext(e -> log.info("doSomethingAsync({}, {});", item, item2))))

Prints:

 doSomethingAsync(a, a);
 doSomethingAsync(a, b);
 doSomethingAsync(a, c);
 doSomethingAsync(b, a);
 doSomethingAsync(b, b);
 doSomethingAsync(b, c);
 doSomethingAsync(c, a);
 doSomethingAsync(c, b);
 doSomethingAsync(c, c);

Upvotes: 1

Related Questions