0

I have a List<Observable<T>> that I would like to transform into an Observable<List<T>>.

I am aware of Observable.zip, which seems like the right function, but I'm not sure how to define the zipper parameter.

Here is what I have tried:

final List<Observable<T>> tasks = getTasks();

final Observable<List<T>> task = Observable.zip(
    tasks, 
    x -> ImmutableList.copyOf(x)
        .stream()
        .map(x -> (T)x)
        .collect(ImmutableList.toImmutableList()));

However, this requires an unchecked cast.

How should I go about this in RxJava 2?


Note that this question refers to RxJava 1.

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245

2 Answers2

1

Use the primitives:

List<Observable<T>> tasks = getTasks();
Observable<List<T>> task = Observable.merge(tasks).toList();

However, do you really need all the tasks at once? You could skip the toList and proccess the tasks as they come; this will give you both better responsiveness and easier concurrency control.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

Something like this should do

final List<Observable<T>> tasks = getTasks();
List<T> values = getTasks().stream()
                           .map(observable -> observable.firstElement().blockingGet())
                           .collect(Collectors.toList());
Observable<List<T>> result = Observable.fromArray(values);
Trash Can
  • 6,608
  • 5
  • 24
  • 38