RxJava2: Conditional and Boolean Operators

Sachin Chandil
3 min readNov 19, 2017

This article demonstrates Conditional and Boolean operators. These operators are simple and easy to understand and gives seamless conditional flow and control over Observables.

Checked out my recent articles on RxJava2:
1. RxJava2: Dispose an Observable chain
2. RxJava2 : Concat + filter

So we will be looking at following Conditional and Boolean Operators:
1. All
2. Amb
3. Contains
4. DefaultIfEmpty
5. SequenceEqual
6. SkipUntil
7. SkipWhile
8. TakeUntil
9. TakeWhile

Lets dive into the examples:

1. All
Determines whether all items emitted by an Observable meet some criteria.

Output:
W/RxJava2 log: onNext: true

All operator works on whole down Stream and returns final value, whether each values emitted by observable meets some criteria. In exampleall operator checks if each items emitted by observable is greater than zero, hence returns true.

2. Amb
Given two or more source Observables, emit all of the items from only the first of these Observables to emit an item.

In this example, observable3 gets executed because observable3 is first to emit its first item. There are three variants of amb:
a. Observable.amb(Iterable<? extends ObservableSource<? extends T>> sources)
b. Observable.ambArray(ObservableSource<? extends T>… sources)
c. Observable.ambWith(ObservableSource<? extends T> other)

Any one of the above operator can be used based on your requirement and coding pattern.

Output:

W/OnNext: next: 41
W/OnNext: next: 42
W/OnNext: next: 43
W/OnNext: next: 44
W/OnNext: next: 45

3. Contains
Determines whether an Observable emits a particular item or not.

It compares all items emitted by observable and returns true or false. It operates on whole stream and returns one value (true/false), unlike filter which further passes value down to the stream for each successful predicate.

Output:

W/OnNext: next: true

4. DefaultIfEmpty
Emits items from the source Observable, or a default item if the source Observable emits nothing.

Given observable in snippet generates a random no. and passes further down if even check validated otherwise completes without emitting any item. Here defaultIfEmpty returns ‘2’ as no item is emitted by observable.

Output:

// -- First run
W/OnNext: Found: 46
W/OnNext: next: 46
// -- Second run (even check failed)
W/OnNext: next: 2

5. SequenceEqual
Determines whether two Observables emit the same sequence of items.

It checks if all the items emitted by given observables match. This operator accepts a predicate function also along with observables to compare custom objects.

Output:

W/OnNext: next: true

6. SkipUntil
Discards items emitted by an Observable until a second Observable emits an item.

skipUntil skips all items emitted by observable1 until observable2 emits its first item. As shows in output, only `3` and `4` were passed down to the stream because second observable starts emitting items after three seconds.

Output:

W/OnNext: next: 3
W/OnNext: next: 4

7. SkipWhile
Discard items emitted by an Observable until a specified condition becomes false.

skipWhile is same as skipUntil but instead of an Observable, skipWhile accepts a predicate. So it skips all items until given predicate turns false.

Output:

W/OnNext: next: 2
W/OnNext: next: 3
W/OnNext: next: 4

8. TakeUntil
Discards items emitted by an Observable after a second Observable emits an item or terminates.

It is just opposite of skipUntil. It takes all items emitted by observable1 until observable2 starts emitting. TakeUntil accepts a predicate also as argument.

Output:

W/OnNext: next: 0
W/OnNext: next: 1

9. TakeWhile

Discards items emitted by an Observable after a specified condition becomes false.

And like skipWhile, takeWhile accepts a predicate and takes all items until predicate fails.

Output:

W/OnNext: next: 0
W/OnNext: next: 1

That’s all for Conditional and Boolean operators.

Please let me know you comments(if any).

If you like this article please hit clap button as many times as you can to support and recommend it to others fellow developers. It means a lot, Thanks.

Happy learning…

Checked out my recent articles on RxJava2:
1. RxJava2: Dispose an Observable chain
2. RxJava2 : Concat + filter

--

--