Press "Enter" to skip to content

RxJava Operators – Part 4: Conditional Operators


RxJava provides some operators that conditionally emit items from the source. We can, for example, skip items to be emitted downstream until a certain condition becomes true. It also contains some operators that make boolean evaluations over items emitted from the source and returns true when a certain item was emitted (or if source satisfies a specified condition).

In this fourth part of our series about RxJava, we will cover some of these operators. They are:

As usual, you can download here a demo app which implements all examples we will demonstrate.

Please, check below for the previous articles of this series:

SkipWhile() Operator

Basically this operator skips items emitted from the source while a certain condition is true. After that, all emitted items are emitted downstream.

Our example is pretty simple. SkipWhile will skip all emitted items until it gets number seven (i.e.: we are calling it as MAGIC_NUMBER). After that, all emitted items will be emitted downstream. We can see it in the doOnNext(…) operator right after skipWhile(). It will only start print messages after skipWhile receives the number seven.

SkipUntil() Operator

If we want to skip items emitted by the source until a second observable emits something, we can use skipUntil. Basically it accepts a second Observable as a parameter and will only emit items from the source when that “other” Observable emits something. It is useful when we need to wait for a task to be finished (the second observable) prior to start a second one (the first observable).

The example below uses a method called doSomeContinuouslyOperation() that simulates a heavy operation by sleeping for some seconds (somewhere between 3 and 6 seconds) and then returns an Observable. Since this method is passed as the skipUntil parameter, while it is being processing, all items emitted by the source will be skipped. After that, emitted items will be emitted downstream.

TakeWhile() Operator

This operator emits downstream items emitted from the source Observable while a certain condition is satisfied. After that, it just completes.

On our example, takeWhile() will mirror source observable while it gets numbers different from seven (again our magic number). When it receives number seven, it will complete. We can see this behavior in the doOnNext() operator (right after takeWhile) that will print emitted numbers while they are not number seven. When number seven is emitted, doOnCompleted() will be called.

TakeUntil() Operator

takeUntil() uses the same approach as the skipUntil, but instead of skip items from the source until a second Observable emits something, it will mirror source Observable.

Our example is quite similar to that one we demonstrated in the skipUntil example. Here, takeUntil will mirror source Observable while doSomeContinuouslyOperation() method execute its operation. After that, takeUntil will complete finishing the whole chain.

Amb() Operator

From the docs: “…amb() operator can have multiple source observables, but will emit all items from ONLY the first of these Observables to emit an item or notification. All the others will be discarded.”. So, we can think of it as a race. The first Observable that emits something wins the race, and only its items will be emitted.

To demonstrate a simple example, we created a helper method called emitItems() that accepts a list of Integers and sleeps for a random time (somewhere between 200 and 1000ms) between its emissions. Since we call this method for both amd() parameters, the instance that first emits an item will “win” the race and all of its items will be emitted. The other Observable will be ignored. To differentiate both Observables, the first list of Integers contains only odd numbers. The second one contains even numbers. By running this example a couple of times, you can see that either only even numbers are emitted or only odd numbers.

All() Operator

All() operator is one of the operators that makes a boolean evaluation over the emitted items. Basically it determines whether all items emitted by an Observable meet some criteria. For example: if we are interested only in even numbers, the first odd number source Observable emits, “all” operator will emit false and complete. On the other hand, if all emitted items are even, it will emit true before completes. This is demonstrated in the example below. emitItems() method will emit 3 random numbers from 1 to 10. If it emits one even number, “all” operator returns false and completes, but if it emits 3 even numbers, “all” returns true before completes:

Contains() Operator

Contains() operator checks the emitted items and emits a boolean indicating whether the source emitted a particular item. If it detects that particular item was emitted, it emits true and completes immediately. Our example is pretty simple. Source emits 5 random numbers, and, if it emits number three, “contains” operator returns true and completes. But if source terminates without emit number three, “contains” will emit false before completes.

Exists() Operator

Quite similar to contains(), but applies a predicate that indicates whether source Observable satisfied a specified condition. As soon as predicate returns true, it emits true and completes. If source observable completes without emitting a value that makes predicate to return true, it returns false prior to complete.

Our example checks whether a number multiple of three was emitted from the source. If it finds it, “exits” operator emits true and completes.Otherwise it emits false before completes.


Conditional operators are extremely useful. If you use reactive programming, chances are you will need them soon (if you haven’t already). They are quite simple to understand and also to use. We hope this article may have helped you somehow.

If you have any question, please, leave a comment below and… stay tuned for the next article about Transforming Operators.