RxJava Ninja: Filtering Operators part 1
This article is a part of the RxJava Ninja series. The complete list of the articles for this series can be found at the end of this article (and will be updated as more articles become available).
Now that we’re done with observable factories, it is time to discuss the operators that makes RxJava a powerful tool. There are hundreds of operators available and in this tutorial we will talk about the filtering operators.
Filtering operators, as the name implies, suppress emissions when a certain condition is not met. The most simple and most common filtering operator is the filter.
Filter
The filter operator takes a predicate as input. It will map each emission to a boolean value and emit only those that qualifies as true.
Let’s see an actual example.
This will output
1
2
ofType
ofType is similar to filter but instead of accepting a predicate, it accepts a type. It only emits those items that are the same as the specified type, or a subtype.
Output:
1
12.0
Take
The take operator allows you to limit the emission count.
One overload of the take operator allows you to limit the count. If the observable emits more than the specified limit, the take operator will simply terminate the emission with an onComplete
.
Output:
1
2
3
completed
If the observable emits fewer items, it will simply emit what is available from the source and terminates as well.
Output:
1
2
3
4
5
completed
The other overload is time based. It takes all the items emitted within the time window.
Output:
0
1
completed
TakeLast
takeLast is the reverse end of take. It emits the last n items from the upstream observable. To do this, it subscribes to the upstream observable and exhaust its elements before emitting the last n items. Therefore it is important to note that this operator is a stateful one. Infinite upstream sequence will block indefinitely.
Output:
beforeTakeLast: 1
beforeTakeLast: 2
beforeTakeLast: 3
beforeTakeLast: 4
beforeTakeLast: 5
beforeTakeLast: 6
5
6
completed
takeLast also has a time based. This operator takes note of the time of each emission and emits the last values that are within the specified time duration.
Output:
beforeTakeLast: 0
beforeTakeLast: 1
beforeTakeLast: 2
beforeTakeLast: 3
beforeTakeLast: 4
beforeTakeLast: 5
beforeTakeLast: 6
beforeTakeLast: 7
beforeTakeLast: 8
beforeTakeLast: 9
beforeTakeLast: 10
beforeTakeLast: 11
beforeTakeLast: 12
beforeTakeLast: 13
beforeTakeLast: 14
beforeTakeLast: 15
beforeTakeLast: 16
beforeTakeLast: 17
beforeTakeLast: 18
beforeTakeLast: 19
15
16
17
18
19
completed
Skip
skip is the opposite of take. It skips the first n emissions and emits the items starting from n + 1, if they exist.
Output:
3
4
5
6
completed
skip also has a time based overload just like take. It skips the emissions within the specified time duration.
SkipLast
skipLast is the opposite of takeLast. Just like takeLast, it also exhausts the upstream observable and queues the items to be able to logically determine the count of items.
Output:
beforeSkipLast: 1
beforeSkipLast: 2
beforeSkipLast: 3
beforeSkipLast: 4
beforeSkipLast: 5
beforeSkipLast: 6
1
2
3
4
completed
Similar to takeLast, skipLast has a time based overload as well.
That’s it for now. There are plenty more filter operators to be discussed so stay tuned.
Check out the other articles in this series.
- Introduction to Reactive Programming
- Building your first Observables and Observers
- In Depth Observables and Observers
- Marble Diagrams and Operators
- Observable Factories Part 1
- Observable Factories Part 2
- Single, Maybe and Completable
- Hot and Cold Observables
- Filtering Operators Part 1
- Filtering Operators Part 2