Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Mono<List<String>> into Flux<String>

I'm converting small project written in RxJava 1.x to Reactor 3.x. All is good, except that I could not find out how to replace flatMap(Observable::from) with an appropriate counterpart. I have Mono<List<String>> and I need to convert it to Flux<String>.

like image 391
Mavo Avatar asked Feb 02 '17 16:02

Mavo


People also ask

How do you convert a list into flux?

2.1. To accomplish the transformation, we would use fromIterable method along with flatMapMany . The fromIterable method of Flux returns FluxIterable that can iterate each item in the provided Iterable. For example, the fromIterable takes the Mono list as input and returns a FluxIterable . Flux<String> flux = Flux.

How do you get flux from mono?

First, we'll extract collection items from stream publisher Mono and then produce items one by one asynchronously as Flux. The Mono publisher contains a map operator, which can transform a Mono synchronously, and a flatMap operator for transforming a Mono asynchronously.

How do I make a list from a mono object?

Using block() is actually the only way to get the object from a Mono when it is emitted. However, you should avoid it at all costs because it defeats the purpose of using the reactive stack given that it actually blocks the thread waiting for the object to be emitted from the Mono .

What is the difference between flux and Mono?

A Flux object represents a reactive sequence of 0.. N items, while a Mono object represents a single-value-or-empty (0..1) result. This distinction carries a bit of semantic information into the type, indicating the rough cardinality of the asynchronous processing.


1 Answers

In Reactor 3, the from operator has been specialized into a few variants, depending on the original source (array, iterable, etc...).

Use yourMono.flatMapMany(Flux::fromIterable) in your case.

like image 174
Simon Baslé Avatar answered Oct 13 '22 22:10

Simon Baslé