Reactor — Convert Flux into List, Map

In this tutorial, I will show you ways to convert Reactor Flux into List/Map that uses collectList(), collectSortedList(), collectMap(), collectMultimap() function.

Ways to convert Flux into Collection

We will use Flux methods such as:

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

Getting Reactor

Reactor installation in Maven

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

<dependencyManagement> 
<dependencies>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-bom</artifactId>
<version>Bismuth-RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

– Next, add dependency:

<dependencies>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>

Reactor installation in Gradle

– First, apply the plugin from the Gradle Plugin Portal:

plugins {
id "io.spring.dependency-management" version "1.0.7.RELEASE"
}

– Next use dependency-management to import the BOM:

dependencyManagement {
imports {
mavenBom "io.projectreactor:reactor-bom:Bismuth-RELEASE"
}
}

– Finally, add dependency:

dependencies {
implementation 'io.projectreactor:reactor-core'
}

Declare & Initialize Flux

There are many ways to initialize a Flux, in this tutorial, we’re gonna use a simple way with Flux.just() function.

Flux<String> flux = Flux.just(
"Site_0:bezkoder.com",
"Description_0:Java Technology",
"Description_1:Project Reactor");

Convert Flux into List

Flux collectList()

collectList() will accumulates sequence into a Mono<List>, then we use block() method to subscribe to the Mono and block indefinitely until a next signal is received.

List<String> list1 = flux.collectList().block();
list1.forEach(System.out::println);

Result:

Site_0:bezkoder.com
Description_0:Java Technology
Description_1:Project Reactor

Flux collectSortedList()

collectSortedList() accumulates sequence and sort into a Mono<List>, then we use block() method to subscribe to the Mono and block it.

List<String> list2 = flux.collectSortedList().block();
list2.forEach(System.out::println);

Result:

Description_0:Java Technology
Description_1:Project Reactor
Site_0:bezkoder.com

Convert Flux into Map

Flux collectMap()

Function prototype:

Mono<Map> collectMap(keyExtractor, valueExtractor)

– First, the function converts sequence into a Mono<Map>.
– Finally, the Mono becomes real List/Map by block() method.

Map<String, String> map1 = flux
.collectMap(
item -> item.split(":")[0],
item -> item.split(":")[1])
.block();
map1.forEach((key, value) -> System.out.println(key + " -> " + value));

Result:

Site_0 -> bezkoder.com
Description_1 -> Project Reactor
Description_0 -> Java Technology

Flux collectMultimap()

Function prototype:

Mono<Map<Object, Collection>> collectMultimap(keyExtractor, valueExtractor)

code>collectMultimap(): convert sequence into a Mono<Map> that each Map’s key can be paired with multi-value (in a Collection).

For example, we’re gonna get a Map with Site and Description as keys:

Map<String, Collection<String>> map2 = flux
.collectMultimap(
item -> item.split("_[0-9]+:")[0],
item -> item.split(":")[1])
.block();
map2.forEach((key, value) -> System.out.println(key + " -> " + value));

Check the result:

Site -> [bezkoder.com]
Description -> [Java Technology, Project Reactor]

Implementation

Technology

– Java 8
– Maven 3.6.1
– Reactor Core 3.1.0 with the Bismuth release train.

Source Code

package com.bezkoder.reactor;import java.util.Collection;
import java.util.List;
import java.util.Map;
import reactor.core.publisher.Flux;public class FluxCollection {
public static void main(String[] args) {
Flux<String> flux = Flux.just("Site_0:bezkoder.com", "Description_0:Java Technology","Description_1:Project Reactor");
System.out.println("=== flux.collectList() ===");
List<String> list1 = flux.collectList().block();
list1.forEach(System.out::println);
System.out.println("\n=== flux.collectSortedList() ===");
List<String> list2 = flux.collectSortedList().block();
list2.forEach(System.out::println);
System.out.println("\n=== flux.collectMap() ===");
Map<String, String> map1 = flux
.collectMap(
item -> item.split(":")[0],
item -> item.split(":")[1])
.block();
map1.forEach((key, value) -> System.out.println(key + " -> " + value));
System.out.println("\n=== flux.collectMultimap() ===");
Map<String, Collection<String>> map2 = flux
.collectMultimap(
item -> item.split("_[0-9]+:")[0],
item -> item.split(":")[1])
.block();
map2.forEach((key, value) -> System.out.println(key + " -> " + value));
}
}

The Result

=== flux.collectList() ===
Site_0:bezkoder.com
Description_0:Java Technology
Description_1:Project Reactor
=== flux.collectSortedList() ===
Description_0:Java Technology
Description_1:Project Reactor
Site_0:bezkoder.com
=== flux.collectMap() ===
Site_0 -> bezkoder.com
Description_1 -> Project Reactor
Description_0 -> Java Technology
=== flux.collectMultimap() ===
Site -> [bezkoder.com]
Description -> [Java Technology, Project Reactor]

Further Reading

Originally published at https://bezkoder.com.

A passionate engineer in software development, especially web, mobile & cross-platform application. I love sharing knowledge by writing blogs & tutorials.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store