RxJava 将发出的值收集到数组
RxJava collect emitted values to array
如何收集从 observable 到数组的发射值?
输入:
Observable.just(1,2,3,4,5,6)
预期输出:
[1,2,3,4,5,6]
有几个选项。最简单的是使用 toList()
:
Observable.just(1,2,3,4,5,6)
.toList()
如果您需要做的不仅仅是将它们收集到一个列表中,您可以使用 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 你会找到关于 collect
更好的解释
如何收集从 observable 到数组的发射值?
输入:
Observable.just(1,2,3,4,5,6)
预期输出:
[1,2,3,4,5,6]
有几个选项。最简单的是使用 toList()
:
Observable.just(1,2,3,4,5,6)
.toList()
如果您需要做的不仅仅是将它们收集到一个列表中,您可以使用 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 你会找到关于 collect