RxJava Observable Buffer


The items emitted by the source Observable is buffered based on the count of items to be buffered. As soon as the buffer is full, the whole bundle buffered is emitted rather than emitting the items one at a time.  A new buffer is created, based on the ‘skip’ attribute, it will know how many items to be skipped to begin buffering again. Thus the Observable will continue bundling and emitting bundles.

In below marble diagram, we show the buffer operator which transforms an Observable that emits items into an Observable that emits buffered collections of those items.

Here is a simple example where an array of persons are emitted as bundle of persons, with each bundle having a count of two persons and then we skip one person before creating a fresh buffer.


package com.javarticles.rx;

import io.reactivex.Observable;

public class ObservableBufferExample {
    public static void main(String[] args) {
        String[] persons = new String[] {"Joe", "Jane", "John", "Phil"};
                .buffer(2, 1)


[Joe, Jane]
[Jane, John]
[John, Phil]

Below diagram show how the source Observable is decorated with the buffer idea.

We show here how each component communicates with other to achieve the bundling and its emission.

Download the source code

This was an example about Observable buffer.

You can download the example here: RxJavaObservableBuffer.zip

Comments are closed.