How to convert nested list in Mono to Flux?

user3595026 :

I am very new to reactive-streams, Can someone help me to convert Mono<MyClass> to Flux<Integer>

I tried something like this -

Flux<Integer> myMethod(Mono<MyClass> homeWork) {
    return homeWork.map(h -> h.multiplicands)
              .flatMapMany(Flux::fromIterable).map(m -> h*m);
}
public class MyClass{
    int multiplier;
    List<Integer> multiplicands;
}

I am expecting the result of multiplier * (each) multiplicand in Flux<Integer> format.

Can you help me with the correct way of doing this?

Ilya Zinkovich :

Transform the instance of MyClass into a Stream<Integer> which contains multiplied integers and then turn Mono<Stream<Integer>> into Flux<Integer>:

Flux<Integer> myMethod(Mono<MyClass> homeWork) {
  return homeWork
           .map(hw -> hw.multiplicands.stream().map(m -> m * hw.multiplier))
           .flatMapMany(Flux::fromStream);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=149176&siteId=1