Chapter 3. Operators and Transformations

The aim of this chapter is to explain fundamentals of RxJava’s operators and how you can compose them to build high-level, easy-to-reason data pipelines. One of the reasons why RxJava is so powerful is the rich universe of built-in operators it provides and the possibility of creating custom ones. An operator is a function that takes upstream Observable<T> and returns downstream Observable<R>, where types T and R might or might not be the same. Operators allow composing simple transformations into complex processing graphs.

For example, the Observable.filter() operator receives items from an upstream Observable but forwards only those matching a given predicate. Conversely, Observable.map() transforms items it receives as they fly through. This allows extracting, enriching, or wrapping original events. Some operators are much more involved. For example, Observable.delay() will pass through events as-is; however, each and every one of them will appear after a fixed delay. Finally, there are operators (like Observable.buffer()) that consume several input events before emitting them, possibly batched.

But even when you recognize how wonderful Rx operators are, the true power comes from combining them together. Chaining several operators, forking stream into multiple substreams and then joining them back is idiomatic and you should feel fairly comfortable with it.

Core Operators: Mapping and Filtering

Operators are typically ...

Get Reactive Programming with RxJava 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.