Exploring RxJava in Android — Operators for Filtering Observables

Anitaa Murthy
ProAndroidDev
Published in
4 min readAug 19, 2018

--

This article is part of RxJava Introduction series. You can checkout the entire series here:

Operators for Filtering Observables

Debounce

This operator only emits an item from an Observable if a particular timespan has passed without it emitting another item.

Sample Implementation: Let’s explain this operator with an example. Let’s say we are implementing a search feature in Android using AutoCompleteTextView. Whenever a user enters a character, we would need to fetch the list of items corresponding to that character. If the user enters 10 characters, and if we are fetching the data from a backend api, then that would mean 10 api calls to the backend. With the debounce() operator, we can specify the wait time (for instance, 2 seconds). Then the Observable, will wait 2 seconds every time the user enters a character in the EditText. If the user types another character before the 2 seconds are up, then the Observable waits another 2 seconds. If the user does not enter another character at the end of 2 seconds, the rest api is called.

There are lots of resources online for this implementation. Please check this link and this link for a sample implementation.

Distinct

This operator suppresses duplicate items emitted by an Observable. The distinct operator works very well with primitive data types. But in order to work with a custom dataType, we need to override the equals() and hashCode() methods.

Sample Implementation: The below code provides an example of distinct() operator. When we pass a list of duplicate integer values, the Observable uses the distinct() operator to emit only unique values from the list.

Output:

onNext: 10
onNext: 20
onNext: 30
onNext: 40
onNext: 70
onNext: 60

ElementAt

This operator emits only one item ’n’ emitted by an Observable. We can specify the position we need to emit using the elementAt operator. For instance, elementAt(0) will emit the first item in the list.

Sample Implementation: In the below code, we can specify a list of integers and using the elementAt() operator, we can fetch the element at that particular index. If the index is not in the list, then nothing will be emitted.

Output

Input: [1, 2, 3, 4, 5, 6]
elementAt index 1
onNext: 2

Filter

This operator emits only those items from an Observable that pass a predicate test.

Sample Implementation: In the below code, for a list of integers from 1 to 6, we add a filter condition that filters only the even numbers and emits those integers.

Output

onNext: 2
onNext: 4
onNext: 6

IgnoreElements

This operator does not emit any items from an Observable but mirrors its termination notification (either onComplete or onError). If you do not care about the items being emitted by an Observable, but you do want to be notified when it completes or when it terminates with an error, you can apply the ignoreElements() operator to the Observable, which will ensure that it will never call the observers’ onNext() methods.

Sample Implementation: In the below implementation, only the onComplete() method will be called once the emission of items is complete. There is no onNext() method.

Output

onSubscribed
Completed

Sample

This operator emits the most recent item emitted by an Observable within periodic time intervals. The Sample operator periodically looks at an Observable and emits whichever item it has most recently emitted since the previous sampling.

Sample Implementation: In the below code, we have a list of integers from 1 to 6. The Observable emits each integer every second. We can use the sample() operator to check the emitted items every 2 seconds and emit the latest value from the Observable.

Output

onNext: 2
onNext: 4

Skip

skip(n) operator suppresses the first n items emitted by an Observable.

Sample Implementation: The below code demonstrates the use of skip() operator. Let’s say we have an Observable that emits the first 10 alphabets and if skip(4) operator is used, it skips the first 4 alphabets from the list and emits only the remaining 6 items.

Output

onNext: E
onNext: F
onNext: G
onNext: H
onNext: I
onNext: J

SkipLast

skipLast(n) operator suppresses the last n items emitted by an Observable.

Sample Implementation: The below code demonstrates the use of skipLast() operator. Let’s say we have an Observable that emits the first 10 alphabets and if skipLast(4) operator is used, it skips the last 4 alphabets from the list and emits only the remaining 6 items.

Output

onNext: A
onNext: B
onNext: C
onNext: D
onNext: E
onNext: F

Take

take(n) operator is the exact opposite of Skip. It emit only the first n items emitted by an Observable.

Sample Implementation: The below code demonstrates the use of take() operator. Let’s say we have an Observable that emits the first 10 alphabets and if take(4) operator is used, it emits the first 4 alphabets from the list and skips the remaining 6 items.

Output

onNext: A
onNext: B
onNext: C
onNext: D

TakeLast

takeLast(n) operator emit only the last n items emitted by an Observable.

Sample Implementation: The below code demonstrates the use of takeLast() operator. Let’s say we have an Observable that emits the first 10 alphabets and if takeLast(4) operator is used, it emits the last 4 alphabets from the list and skips the remaining 6 items.

Output

onNext: G
onNext: H
onNext: I
onNext: J

That’s it guys! This is part third of the series on RxJava. I hope you enjoyed this article and found it useful, if so please hit the Clap button. Let me know your thoughts in the comments section.

Happy coding!

--

--