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

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.

主站蜘蛛池模板: 怀安县| 浦东新区| 宝坻区| 无锡市| 伊吾县| 威远县| 阿瓦提县| 鹤山市| 沂南县| 肥乡县| 沽源县| 阿拉善右旗| 永德县| 武平县| 潮州市| 麦盖提县| 滨海县| 绥化市| 凌云县| 安宁市| 普格县| 涿鹿县| 磐安县| 平原县| 通山县| 祁阳县| 喀喇| 宿迁市| 健康| 中宁县| 抚宁县| 祁门县| 寿阳县| 四会市| 兴义市| 宽甸| 泾源县| 桐城市| 桦南县| 大理市| 乌鲁木齐县|