February 2020
Intermediate to advanced
412 pages
9h 36m
English
The filter() operator accepts Predicate<T> for a given Observable<T>. This means that you provide it a lambda that qualifies each emission by mapping it to a Boolean value, and emissions with false will not go downstream.
For instance, you can use filter() to only allow string emissions that are not five characters in length:
import io.reactivex.rxjava3.core.Observable;public class Ch3_05 { public static void main(String[] args) { Observable.just("Alpha", "Beta", "Gamma") .filter(s -> s.length() != 5) .subscribe(s -> System.out.println("RECEIVED: " + s)); }}
The output of the preceding code snippet is as follows:
RECEIVED: Beta
The filter() operator is probably the most commonly used to suppress emissions.
Read now
Unlock full access