2

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]
LeTadas
  • 3,436
  • 9
  • 33
  • 65

1 Answers1

2

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

Fred
  • 16,367
  • 6
  • 50
  • 65