Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 311
user3595026 Avatar asked Sep 06 '25 03:09

user3595026


1 Answers

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);
}
like image 173
Ilya Zinkovich Avatar answered Sep 08 '25 00:09

Ilya Zinkovich