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()
Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
Giorgio
  • 13,129
  • 12
  • 48
  • 75

2 Answers2

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.

Tim
  • 41,901
  • 18
  • 127
  • 145
Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
4

you can use this

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