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

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.

主站蜘蛛池模板: 河间市| 临海市| 拉孜县| 浪卡子县| 乌什县| 南川市| 马鞍山市| 二手房| 崇左市| 佛山市| 讷河市| 安新县| 五华县| 西乌| 颍上县| 南木林县| 泰州市| 玛沁县| 红河县| 象山县| 辛集市| 翁源县| 南华县| 五家渠市| 舟山市| 太康县| 余庆县| 南开区| 家居| 教育| 延长县| 青河县| 伊金霍洛旗| 平江县| 舞钢市| 莫力| 潞西市| 从化市| 清流县| 涞源县| 梧州市|