- 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.
推薦閱讀
- 大學計算機基礎(第二版)
- Learn TypeScript 3 by Building Web Applications
- Java應用與實戰
- Visual FoxPro程序設計教程(第3版)
- 跟小海龜學Python
- 新編Premiere Pro CC從入門到精通
- 深度學習:算法入門與Keras編程實踐
- Java實戰(第2版)
- Multithreading in C# 5.0 Cookbook
- C/C++數據結構與算法速學速用大辭典
- RubyMotion iOS Develoment Essentials
- 新印象:解構UI界面設計
- 遠方:兩位持續創業者的點滴思考
- 軟件測試技術
- SAP Web Dynpro for ABAP開發技術詳解:基礎應用