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

2

66

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()
Chiquita answered 16/4, 2015 at 11:0 Comment(0)
B
124

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.

Barite answered 16/4, 2015 at 11:10 Comment(0)
B
4

you can use this

flatMap { t -> Observable.fromIterable(t) }
Blanca answered 3/7, 2019 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.