Reactor – How to convert Flux into List, Map

Reactor – How to convert Flux into List, Map

In this tutorial, JavaSampleApproach introduces ways to convert Reactor Flux into List/Map.

I. Ways to convert Flux into Collection

We will use Flux methods such as:
collectList(): accumulate sequence into a Mono<List>.
collectSortedList(): accumulate sequence and sort into a Mono<List>.
collectMap(): convert sequence into a Mono<Map>.
collectMultimap(): convert sequence into a Mono<Map> that each Map’s key can be paired with multi-value (in a Collection).

Then the Mono result above will be converted into a real List/Map using block() method.

0. Initialization
0.1 Reactor installation in Maven

– First, import the BOM by adding the following to pom.xml:

– Next, add dependency:

0.2 Declare & Initialize Flux

1. List
1.1 Flux.collectList

1.2 Flux.collectSortedList

2. Map
2.1 Flux.collectMap

2.2 Flux.collectMultimap

II. Source Code

1. Technology

– Java 8
– Maven 3.6.1
– Reactor Core 3.0.4, with the Aluminium release train.

2. Code

3. Results

By grokonez | June 29, 2017.

Last updated on April 16, 2020.

Related Posts

1 thought on “Reactor – How to convert Flux into List, Map”

  1. I think that not working now because you should get error:
    block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-3

Got Something To Say:

Your email address will not be published. Required fields are marked *