February 2020
Intermediate to advanced
412 pages
9h 36m
English
For a given Observable<T>, the startWithItem() operator (previously called startWith() in RxJava 2.x) allows you to insert a value of type T that will be emitted before all the other values. For instance, if we have an Observable<String> that emits drink names we would like to print, we can use startWithItem() to insert a header as the first value of the stream:
import io.reactivex.rxjava3.core.Observable;public class Ch3_15 { public static void main(String[] args) { Observable<String> menu = Observable.just("Coffee", "Tea", "Espresso", "Latte"); //print menu menu.startWithItem("COFFEE SHOP MENU") .subscribe(System.out::println); }}
The output of the preceding code snippet is as follows:
COFFEE SHOP MENUCoffeeTeaEspresso ...
Read now
Unlock full access