I have a list of Observables (RxJava 1).
List<Observable> observableList = new ArrayList<>();
It can contain at least 1 Observable. Each has the same type of the result.
How can I zip results of the all Observables?
I thought about zip-operator but it doesn't support List and I don't know quantity of observables (it can be 1,2,3,4....)
I struggled with this as well, and used Sharan's solution as a base for mine.
My use case was doing API calls to several 3rd party providers, and then putting each individual result in a List. Each item in the list contains what the API returned, be it success or failure.
In the end it actually looks quite elegant. In my specific case "ResultType" was replaced with something like "ApiGenericResponseObject".
Alternatively, as a Lambda it looks neater. Though I wonder whether someone reading this will understand what is going on:
Hope it helps!
ReactiveX - Zip operator
Zip beyond BiFunction
Here, list is an Array List of Observables of whichever type you want to pass.
The Result of the following subscription is this image below. Just like we expect it to be zipped together. Also can you notice it returns all the responses to be zipped in a single java.lang.Object[].
Note I had to type cast my Array List to access my single object because it is of type Any!
You can use the static
zip(java.lang.Iterable<? extends Observable<?>> ws,FuncN<? extends R> zipFunction)
method.It is a
zip
method that takes anIterable
ofObservable
s and aFuncN
(which takes a varargs parameter for itscall
method) and uses it to combine the corresponding emittedObject
s into the result to be omitted by the new returnedObservable
.So for example you could do: