Convert Observable<List<Car>> to a sequence of Observable<Car> in RxJava

喜你入骨 提交于 2019-12-03 03:25:45

问题


Given a list of cars (List<Car> cars), I can do:

Observable.just(cars); //returns an Observable that emits one List<Car>
Observable.from(cars); //returns an Observable that emits a squence of Car

Is there a way I can go from an Observable of a List<Car> to a sequence of Observable<Car>?

Something like a from without parameters

Obserable.just(cars).from()

回答1:


You can map an Observable<List<Car>> to Observable<Car> like so:

yourListObservable.flatMapIterable(x -> x)

Note that flatMapping might not preserve the order of the source observable. If the order matters to you, use concatMapIterable. Read here for more details.




回答2:


you can use this

flatMap { t -> Observable.fromIterable(t) }


来源:https://stackoverflow.com/questions/29672705/convert-observablelistcar-to-a-sequence-of-observablecar-in-rxjava

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!