The buffer operator

The buffer() operator gathers all Flux<T> elements and emits them as a List<T>. Unlike groups generated by the groupBy() operator, all the elements in the List<T> buffer are in their original order. Alternatively, we could provide a batchSize to the operator. The operator will then generate N lists, each of which will have a specified number of elements. Let's try to use the buffer operator on our Fibonacci series:

    @Test    public  void testBufferWithDefinateSize(){        Flux<Long> fibonacciGenerator = Flux.generate(() -> Tuples.<Long,                Long>of(0L, 1L), (state, sink) -> {            if (state.getT1() < 0)                sink.complete();            else                sink.next(state.getT1());            return Tuples.of(state.getT2(), state.getT1() + state.getT2());        }); fibonacciGenerator.take(100) ...

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.