June 2017
Intermediate to advanced
400 pages
8h 44m
English
If we want to resort to a single emission if a given Observable comes out empty, we can use defaultIfEmpty(). For a given Observable<T>, we can specify a default T emission if no emissions occur when onComplete() is called.
If we have an Observable<String> and filter for items that start with Z but no items meet this criteria, we can resort to emitting None:
import io.reactivex.Observable;public class Launcher { public static void main(String[] args) { Observable<String> items = Observable.just("Alpha","Beta","Gamma","Delta","Epsilon"); items.filter(s -> s.startsWith("Z")) .defaultIfEmpty("None") .subscribe(System.out::println); }}
The output of the preceding code snippet is as follows:
None
Of course, if emissions were to ...
Read now
Unlock full access