Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the first element from flux without converting flux into stream?

Is it possible to get the first element from flux without converting flux into stream?

like image 406
akreddy.21 Avatar asked Mar 05 '19 11:03

akreddy.21


People also ask

How do you get data from flux?

How to extract data from Flux in Java? Another way would be using the Reactive Streams operators like onNext, flatMap, etc. In the below example, we are using the onNext() method to get data from Flux and print it. Note: We need to subscribe to the Publisher.

How do you get mono from Flux?

Instead of take(1) , you could use next() . This will transform the Flux into a valued Mono by taking the first emitted item, or an empty Mono if the Flux is empty itself.

What is the difference between mono and flux?

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.

Does flux subscribe block?

Subscribe to this Flux and block until the upstream signals its first value, completes or a timeout expires. Subscribe to this Flux and block indefinitely until the upstream signals its last value or completes.


1 Answers

Alternatively to take(1), if you need a Mono<T> that represents the first element of the Flux<T> you can use .next().

Or if you need the i-th element, use .elementAt(i) (must be sure that such an element exists though, unlike take and next which just return an empty publisher if not enough elements).

like image 153
Simon Baslé Avatar answered Oct 04 '22 01:10

Simon Baslé