How can I collect emitted values from observable to array?
Input:
Observable.just(1,2,3,4,5,6)
Expected output:
[1,2,3,4,5,6]
There's a couple of options. Easiest is to use toList()
:
Observable.just(1,2,3,4,5,6)
.toList()
If you need to do more than just collect them into a list you can use collect()
:
List<Integer> collected = new ArrayList<>();
Observable.just(1,2,3,4,5,6)
.collect(collected, (alreadyCollected, value) -> {
// Do something with value and add it to collected at the end
});
Here you'll find a better explanation about collect