我的目标是解包一个整数列表的单个列表,并获取它的所有元素,以便将它放在一个列表中。
List<Single<List<Int>>> listOfSinglesOfListofInts = getListSingleListType(); // random method that gives us that.
List<Int> intList = new ArrayList<>();
我的目标是将所有 Int 内容从 listOfSinglesOfListOfInts 移动到 listInt。这是我尝试过的:
ListOfSinglesOfListOfInt.stream().map(
singleListOfInts -> singleListOfInts.map(
listOfInts -> intList.addAll(listOfInts)
)
);
return listInt;
listInt 的大小始终为 0。
完成此任务的正确方法是什么?
最佳答案
map
操作直到 Flowable
才运行链完成。这Flowable
已设置,但未执行。您可能想要做的是运行 Flowable
压平形状后通过阻塞收集器。试试这个:
return Flowable.fromIterable(listOfSingleOfListofInt)
.flatMap(singleOfListofInt -> singleOfListofInt.toFlowable())
.flatMap(listofInt -> Flowable.fromIterable(listofInt))
.toList()
.blockingGet();
详情
Flowable.fromIterable(listOfSingleOfListofInt)
:
- 转换
List<Single<List<Int>>>
进入Flowable<Single<List<Int>>>
flatMap(singleOfListofInt -> singleOfListofInt.toFlowable())
:
- 转换
Flowable<Single<List<Int>>>
进入Flowable<List<Int>>
flatMap(listofInt -> Flowable.fromIterable(listofInt))
:
- 转换
Flowable<List<Int>>
进入Flowable<Int>
toList()
:
- 转换
Flowable<Int>
进入Signle<List<Int>>
blockingGet()
- 转换
Signle<List<Int>>
进入List<Int>
关于java - 如何从 List<Single<List<Int>>> 中收集所有整数并将其放入 RxJava 中的 List<Int> 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53055009/