RxJava Ninja: Filtering Operators part 2
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).
Let us now continue with our discussions of Filtering operators.
First
first converts the observable to a Single
and emits the first item, or a given default value if the source does not emit any items.
Output:
1
There is also a variant that converts to a Maybe
called firstElement. This emits only the first item emitted by the source. A variant called singleElement also exists which emits the first item but throws an exception if there are more than 1 item.
ElementAt
elementAt returns a Maybe
that emits the element at the specified index. If the source terminates before the specified index, elementAt will simply complete. Indexes are zero based.
Output:
5
Last
last converts an observable to a Single
and emits the last item emitted by the source before it completes, or a default value if the source did not emit anything. In other implementations (such as .NET), this is a blocking operator that returns the last value, not an observable.
Output:
5
There is also a variant called lastElement that converts to Maybe
. This will simply complete if no items are emitted by the source.
TakeWhile
takeWhile is a variant of take which emits a value while a given predicate holds true.
Output:
1
2
3
SkipWhile
skipWhile is a variant of skip that prevents emission while a given predicate holds true.
Output:
3
4
5
IgnoreElements
This operator simply ignores all emissions and emits complete or error events only.
This code outputs nothing.
Sample
sample is a very powerful operator. It allows you to get the most recent item emitted by the source at specific intervals.
Output:
1
3
4
7
8
10
13
...
Debounce
debounce emits the last item after a specified time interval has elapsed since the last emission. In simpler terms, it filters out rapid emission. This is useful in cases such as monitoring text fields for real-time search. When searching, you do not want to trigger a search every time the search text updates. You might want to trigger only after a time interval has passed since the last emission.
This is not a good example but it demonstrates the function. This will emit:
5
Distinct
distinct is both powerful and dangerous when used incorrectly. distinct only emits unique values and filters duplicates. Equality is judged using the default Object.equals() and Object.hashCode() methods so be sure to define meaningful comparison logic.
Internally, distinct uses a HashSet
to store the unique references. Distinct does not clear this map until the sequence terminates so this will only grow. Unbounded or infinite streams with very distinct elements may cause this HashSet
to grow very large and cause an OutOfMemoryError
.
Output:
1
2
3
4
5
There are other variants that improves the default functionality. One variant allows you to provide a key selector function to determine uniqueness. Another variant allows you to provide a collection supplier to control retention policy manually.
DistinctUntilChanged
This operator is a special version of the distinct operator. It allows you to emit values that are different from their predecessor. This operator caches the previous emitted item for comparison with the succeeding items. It also uses the default Object.equals() and Object.hashCode() to determine uniqueness.
Output:
1
2
1
2
3
4
5
There is also a variant wherein you can provide a BiPredicate that receives the previous and current items. This will allow you to determine uniqueness manually.
That’s it for the Filtering operators. We will now move on with other types of operators on the next articles.
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