I am using RxJava.
I have an Observable<T>
. How do I convert it to List<T>
?
Seems to be a simple operation, but I couldn't find it anywhere on the net.
Hope this helps.
List<T> myList = myObservable.toList().toBlocking().single();
thanks
anand raman
You can use toList() or toSortedList() . For e.g.
observable.toList(myObservable)
.subscribe({ myListOfSomething -> do something useful with the list });
RxJava 2+:
List<T> = theObservarale
.toList()
.blockingGet();
You can also use the collect
operator:
ArrayList list = observable.collect(ArrayList::new, ArrayList::add)
.toBlocking()
.single();
With collect
, you can choose which type of Collection
you prefer and perform an additional operation on the item before adding it to the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With