The collect operator

Reactor also provides operators that make it possible to accumulate data streams as collections. The most basic of these is the collectList() operator. The operator accumulates the data as a list, as shown in the following diagram:

Let's take our Fibonacci example and collect the data into a list. The collector method provides a Mono publisher that will emit a single list containing all of the published data:

public void testFibonacciCollect() {
    Flux<Long> fibonacciGenerator = Flux.generate(
            () -> Tuples.<Long, Long>of(0L, 1L),
            (state, sink) -> {
                sink.next(state.getT1());
                return Tuples.of(state.getT2(), state.getT1() ...

Get Hands-On Reactive Programming with Reactor now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.