Everything you should know about Rx Operators

Keval Patel
6 min readJan 4, 2017

RxJava is taking boom in the android development. It’s the hot topic among new and experienced developers. If you don’t know what the Reactive programming is and what are the benefits of using it, I will strongly encourage you to go through the first article of this series: What is Reactive Programming?

Reactive programming us useful in two ways.

  • It provides a super easy interface to handle concurrency and thread management in your application. Also, it keeps your application code much cleaner and readable. If you don’t know how to use RxJava in your Java or Android project, it’s recommended that first, you go through Code your next app using RxJava.
  • Use of Rx operators. With RxOperator are basically a function that defines the observable, how and when it should emit the data stream. There are hundreds of operators available in RxJava. You can read an alphabetical list of all the operators available from here. I cannot cover all the useful operators in a single article. So, I am going to divide it into two parts. In this and next upcoming article we are going to take a look in some of the most useful operators with marble diagram of each operator.

Fun Fact:

  • Most operators operate on an Observable and return an Observable. This allows you to apply these operators one after the other, in a chain. Each operator in the chain modifies the Observable that results from the operation of the previous operator.
  • A chain of Observable operators do not operate independently on the original Observable that originates the chain, but they operate in turn, each one operating on the Observable generated by the operator immediately previous in the chain.

Let’s start with our first operator.

There are many duplicate operators, which have almost same functionality. I will try to cover them at the same time.

Creating Observables

1. just():

As the name suggest, just operator emits the same values provided in the arguments. Nothing else.

just() operator marble diagram

Here our observable has just operator applied. So, observable will emit 1 to 5 integers, one after another.

  • There is another operator from(), which takes array of an object as input and emits the object one after another same as just() operator. Below is the code snippets to emit 1 to 5 integer number using from() operator.

Filtering Operators:

  • Filtering operators will filter out the data stream objects based on some expression and only emits data objects those satisfy the expression.
  • Here, I am going to explain some of them. You can find complete list of Filtering Observables from here.

1. filter() :

Sometimes we want to refine specific event only to be emitted by observable. Let’s say in our above example we only want to emit only odd numbers out of the observable. We can achieve this thing using another operator called filter().

filter() operator marble diagram

As the name suggest filter operator filters items emitted by an Observable. All you have to do is tell the operator, weather to emit the object or not based on some conditions.

2. skip():

skip(n) will suppress the first n items emitted by an Observable and emits data after n elements. So, skip(2) will emit first 2 elements and starts from emitting the 3rd element.

skip() operator marble diagram

So, for our example, if we apply skip(2) operator, it will emit only 3, 4 and 5 integers.

  • There is another operator skipLast(). This operator will emit only last element of the data stream.

3. take():

take() is counter to the skip() operator. take(n) will emit only first n data elements and ignores all data elements after n elements emitted.

take() operator marble diagram
  • takeLast() operator will emit only the last element from the data stream.
  • Opposite to above, takeFirst() operator will emit only first element of the data stream and ignores subsequent data elements.

Combining Operators:

  • Combining operators combines more than two data streams emitted by different observable and emit single data stream.
  • You can find complete list of combining operators from here.

1. concat():

As the name suggest, you can use concat() operator to concat two different observable and emit the data stream for both the operators one after another.

Let say you have two observable. Each of them emits from integer 1 to 5 and 6 to 10 respectively. If we concat them, observable 1 will emit from 1 to 5 and observable 2 will emit data from 6 to 10 simultaneously. concat() operator will merge both the data stream and emit data from observable 1 and then after it will emit data from observable 2. Below is the marble diagram which explains the same concept.

2. merge():

merge() operator works same as the concat() operator and combines data stream from two different observables. The only difference between merge() and concat() operator is merge can interleave the outputs, while concat will first wait for earlier streams to finish before processing later streams.

So, unlike concat() operator merge() operator doesn’t wait for data from observable 1 to complete. It emits data from both the observable simultaneously as soon as the data becomes available to emit.

3. zip():

zip() operator combine the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function.

zip() operator marble diagram

Here is the example where you can combine strings and integer data streams into single ZipObject (custom class) and emit them as the final data stream.

You can suggest me Rx operators those you want to see in comments below. I will try to cover them in next (and last) part.

https://paypal.me/kpatel2106?locale.x=en_GB

If you liked the article, click the 💚 below so more people can see it! Make sure that you follow me on Medium or on Twitter to get update whenever new article gets published.

--

--

Keval Patel

www.kevalpatel2106.com | Android Developer | Machine learner | Gopher | Open Source Contributor