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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.