- Learning RxJava
- Thomas Nield
- 127字
- 2021-07-02 22:22:58
cast()
A simple, map-like operator to cast each emission to a different type is cast(). If we want to take Observable<String> and cast each emission to an object (and return an Observable<Object>), we could use the map() operator like this:
Observable<Object> items =
Observable.just("Alpha", "Beta", "Gamma").map(s -> (Object) s);
But a shorthand we can use instead is cast(), and we can simply pass the class type we want to cast to, as shown in the following code snippet:
Observable<Object> items =
Observable.just("Alpha", "Beta", "Gamma").cast(Object.class);
If you find that you are having typing issues due to inherited or polymorphic types being mixed, this is an effective brute-force way to cast everything down to a common base type. But strive to properly use generics and type wildcards appropriately first.
推薦閱讀
- 極簡算法史:從數學到機器的故事
- WebAssembly實戰
- Leap Motion Development Essentials
- Bulma必知必會
- HTML5+CSS3+JavaScript Web開發案例教程(在線實訓版)
- Hands-On RESTful Web Services with Go
- Elasticsearch for Hadoop
- VMware虛擬化技術
- Python Data Structures and Algorithms
- Scala Data Analysis Cookbook
- 響應式Web設計:HTML5和CSS3實戰(第2版)
- SQL Server 2008 R2數據庫技術及應用(第3版)
- Laravel Application Development Blueprints
- Applied Deep Learning with Python
- After Effects CC案例設計與經典插件(視頻教學版)