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

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.

主站蜘蛛池模板: 东阳市| 金昌市| 天水市| 马关县| 太保市| 彭水| 卢龙县| 饶河县| 定远县| 临湘市| 巴青县| 长治市| 女性| 镶黄旗| 镶黄旗| 安康市| 临漳县| 石棉县| 溧水县| 靖州| 洱源县| 舞阳县| 繁峙县| 英超| 错那县| 霍邱县| 黄大仙区| 红安县| 三都| 鄂托克前旗| 千阳县| 巴中市| 台东市| 彩票| 长海县| 宜宾市| 洪洞县| 大丰市| 赤壁市| 平武县| 宁南县|