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

JavaReactive ProgrammingRx Java

Java Problem Overview


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()

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

you can use this

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGiorgioView Question on Stackoverflow
Solution 1 - JavaEgor NeliubaView Answer on Stackoverflow
Solution 2 - JavaMohammad Sultan Al NahiyanView Answer on Stackoverflow