- 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.
- Spring Cloud Alibaba核心技術與實戰案例
- Facebook Application Development with Graph API Cookbook
- Reporting with Visual Studio and Crystal Reports
- Vue.js前端開發基礎與項目實戰
- 羅克韋爾ControlLogix系統應用技術
- Python程序設計案例教程
- Silverlight魔幻銀燈
- 微信小程序全棧開發技術與實戰(微課版)
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- C/C++數據結構與算法速學速用大辭典
- 創意UI Photoshop玩轉移動UI設計
- Photoshop智能手機APP界面設計
- Java Hibernate Cookbook
- Python預測之美:數據分析與算法實戰(雙色)
- Splunk Essentials