RxJava Operators allows you manipulate the data emitted by Observables. Basically, operators tells Observable, how to modify the data and when to emit the data. Using the operators you can modify, merge, filter or group the data streams.

RxJava has vast collection of powerful operators that are categorized depending on the purpose they serve. You can achieve any complex data stream by combining multiple operators together.

Types of Operators

All the operators are categorized depending on the kind of work it do. Some operators are used to Create Observables. The operators like create, just, fromArray, range creates an Observable.

Some operators such as debounce, filter, skip, last are used to filter the data emitted by an Observable. The operators like buffer, map, flatMap, switchMap, compose creates an Observable by transform the data emitted by another Observable.

You can see the list of Operators categorized on this page.

Let’s consider the example below. Here an Observable is created using fromArray() operator which emits the numbers from 1 to 20.

Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

        Observable.fromArray(numbers)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<Integer>() {
                    @Override
                    public void onNext(Integer integer) {
                        Log.d(TAG, "Number: " + integer);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "All numbers emitted!");
                    }
                });

The above Observable prints the output as below.

Number: 1
Number: 2
Number: 3
.
.
.
Number: 19
Number: 20
All numbers emitted!

Instead of writing the array of numbers manually, you can do the same using range(1, 20) operator as below.

Observable.range(1, 20)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<Integer>() {
                    @Override
                    public void onNext(Integer integer) {
                        Log.e(TAG, "Number: " + integer);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "All numbers emitted!");
                    }
                });

Chaining of Operators

Sometimes the desired data stream can’t achieved using a single operator. In that case you can use multiple operators together. When multiple operators are used, the operators takes the result from the previous operator.

Let’s take same example of emitting numbers from 1 to 20. But in this case we want to filter out the even numbers along with we want to append a string at the end of each number.

  • range(): Range operator generates the numbers from 1 to 20
  • filter(): Filters the numbers by applying a condition onto each number
  • map(): Map transform the data from Integer to String by appending the string at the end
  • In the operator chain, filter() will be executed first and map() takes the result from filter and performs it’s job
Observable.range(1, 20)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new Predicate<Integer>() {
                    @Override
                    public boolean test(Integer integer) throws Exception {
                        return integer % 2 == 0;
                    }
                })
                .map(new Function<Integer, String>() {
                    @Override
                    public String apply(Integer integer) throws Exception {
                        return integer + " is even number";
                    }
                })
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {   
                    }

                    @Override
                    public void onNext(String s) {
                        Log.d(TAG, "onNext: " + s);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "All numbers emitted!");
                    }
                });
onNext: 2 is even number
onNext: 4 is even number
onNext: 6 is even number
onNext: 8 is even number
onNext: 10 is even number
onNext: 12 is even number
onNext: 14 is even number
onNext: 16 is even number
onNext: 18 is even number
onNext: 20 is even number
All numbers emitted!

RxJava Operators

As RxJava got extensive arsenal of operators, it’s better each operator is covered in a separate article.

  1. Just
  2. From
  3. Range
  4. Buffer
  5. Debounce
  6. Filter
  7. Repeat
  8. Skip
  9. Take
  10. TakeLast
  11. Distinct
  12. Count
  13. Reduce
  14. Max
  15. Min
  16. Sum
  17. Average
  18. Concat
  19. Merge
  20. Map
  21. FlatMap
  22. ConcatMap
  23. SwitchMap
Author

  • pradeepkumar reddy

    The chaining of operators should be done in a sequence so that the output of one operator will be taken as input to next operator. is it so ?