DEV Community

Cover image for Convert Flux to List, Map - Reactor
bezkoder
bezkoder

Posted on • Updated on • Originally published at bezkoder.com

Convert Flux to List, Map - Reactor

Original Post: https://bezkoder.com/reactor-flux-list-map/

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

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.

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");
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Result:

Site_0:bezkoder.com
Description_0:Java Technology
Description_1:Project Reactor
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Result:

Description_0:Java Technology
Description_1:Project Reactor
Site_0:bezkoder.com
Enter fullscreen mode Exit fullscreen mode

Convert Flux into Map

Flux collectMap()

Function prototype:

Mono<Map> collectMap(keyExtractor, valueExtractor)
Enter fullscreen mode Exit fullscreen mode

– 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));
Enter fullscreen mode Exit fullscreen mode

Result:

Site_0 -> bezkoder.com
Description_1 -> Project Reactor
Description_0 -> Java Technology
Enter fullscreen mode Exit fullscreen mode

Flux collectMultimap()

Function prototype:

Mono<Map<Object, Collection>> collectMultimap(keyExtractor, valueExtractor)
Enter fullscreen mode Exit fullscreen mode

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));
Enter fullscreen mode Exit fullscreen mode

Check the result:

Site -> [bezkoder.com]
Description -> [Java Technology, Project Reactor]
Enter fullscreen mode Exit fullscreen mode

Implementation

Technology

– Java 8
– Maven 3.6.1
– Reactor Core 3.4.0 with the 2020.0.1 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));
    }
}
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

Appendix: 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>2020.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Enter fullscreen mode Exit fullscreen mode

– Next, add dependency:

<dependencies>
  <dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
  </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Reactor installation in Gradle

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

plugins {
  id "io.spring.dependency-management" version "1.0.7.RELEASE" 
}
Enter fullscreen mode Exit fullscreen mode

– Next use dependency-management to import the BOM:

dependencyManagement {
  imports {
    mavenBom "io.projectreactor:reactor-bom:2020.0.1"
  }
}
Enter fullscreen mode Exit fullscreen mode

– Finally, add dependency:

dependencies {
  implementation 'io.projectreactor:reactor-core' 
}
Enter fullscreen mode Exit fullscreen mode

Further Reading

Top comments (0)