官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 胶南市| 旬邑县| 福泉市| 阜康市| 甘泉县| 阳东县| 邯郸市| 黄石市| 都安| 望城县| 新竹市| 修武县| 绥阳县| 吴旗县| 翼城县| 怀宁县| 三亚市| 阳春市| 内江市| 府谷县| 台州市| 邓州市| 嘉义县| 光泽县| 基隆市| 沽源县| 高安市| 富裕县| 昔阳县| 垣曲县| 互助| 扎兰屯市| 宿迁市| 吴桥县| 博野县| 清新县| 闽侯县| 西乌| 临洮县| 永清县| 南丰县|