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

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.

主站蜘蛛池模板: 宣恩县| 板桥市| 兰西县| 延吉市| 庆云县| 交城县| 胶州市| 蓝田县| 海宁市| 花莲市| 开化县| 凭祥市| 广河县| 达拉特旗| 惠水县| 英山县| 连平县| 晴隆县| 民乐县| 浑源县| 澎湖县| 甘孜县| 丹凤县| 竹北市| 永平县| 梧州市| 长春市| 大名县| 四川省| 西林县| 临沧市| 清远市| 岢岚县| 吴川市| 根河市| 基隆市| 武陟县| 侯马市| 延津县| 琼结县| 东乌珠穆沁旗|