Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Mono<Map<String, Optional<String>>> to a Flux<Tuple<String, Optional<String>>>

I'm using WebFlux with Reactor and creating a rest service that aggregates and transforms results from an internal service call using WebFlux webclient.

The client returns a Mono<Map<String, Optional<String>>> which I want to disassemble in a Flux with key, value elements so that I can combine it later on.

I was thinking of converting the client response of type

Mono<Map<String, Optional<String>>> to a

Flux<Tuple<String, String>>

I know you can convert a Mono<List<String>> to a Flux<String> using responseMono.flatMapMany(Flux::fromIterable)

but I couldn't find a way to handle Maps.

like image 503
sindica Avatar asked Oct 30 '25 04:10

sindica


1 Answers

You can use mono.flatMapMany(map -> Flux.fromIterable(map.entrySet())) which will give you a Flux<Entry<String, Optional<String>>>.

If you explicitly want it to be a Tuple, not an Entry, then just do:

mono.flatMapMany(map -> Flux.fromIterable(map.entrySet()))
        .map(e -> Tuples.of(e.getKey(), e.getValue()));
like image 91
Michael Berry Avatar answered Nov 01 '25 12:11

Michael Berry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!