简体   繁体   中英

How to convert nested list in Mono to Flux?

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM