- Learning RxJava
- Thomas Nield
- 236字
- 2021-07-02 22:22:58
startWith()
For a given Observable<T>, the startWith() operator allows you to insert a T emission that precedes all the other emissions. For instance, if we have an Observable<String>that emits items on a menu we want to print, we can use startWith() to append a title header first:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable<String> menu =
Observable.just("Coffee", "Tea", "Espresso", "Latte");
//print menu
menu.startWith("COFFEE SHOP MENU")
.subscribe(System.out::println);
}
}
The output of the preceding code snippet is as follows:
COFFEE SHOP MENU
Coffee
Tea
Espresso
Latte
If you want to start with more than one emission, use startWithArray() to accept varargs parameters. If we want to add a divider between our header and menu items, we can start with both the header and divider as emissions:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable<String> menu =
Observable.just("Coffee", "Tea", "Espresso", "Latte");
//print menu
menu.startWithArray("COFFEE SHOP MENU","----------------")
.subscribe(System.out::println);
}
}
The output of the preceding code snippet is as follows:
COFFEE SHOP MENU
----------------
Coffee
Tea
Espresso
Latte
The startWith() operator is helpful for cases like this, where we want to seed an initial value or precede our emissions with one or more emissions.
- Mastering Zabbix(Second Edition)
- Web交互界面設(shè)計(jì)與制作(微課版)
- Apache Spark Graph Processing
- 飛槳PaddlePaddle深度學(xué)習(xí)實(shí)戰(zhàn)
- Node.js全程實(shí)例
- R語言與網(wǎng)絡(luò)輿情處理
- 微信小程序開發(fā)與實(shí)戰(zhàn)(微課版)
- 區(qū)塊鏈國產(chǎn)化實(shí)踐指南:基于Fabric 2.0
- Software Architecture with Python
- Unity 5 Game Optimization
- Learning Ionic(Second Edition)
- 中小企業(yè)網(wǎng)站建設(shè)與管理(靜態(tài)篇)
- Learning SaltStack(Second Edition)
- Prezi Cookbook
- C#程序開發(fā)教程