2 回答
TA贡献1812条经验 获得超5个赞
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>
TA贡献1785条经验 获得超8个赞
我在 Rx-Java 中做到了。
Lets consider below example to create List<Single<List<Integer>>>
List<Integer> intList = new ArrayList<>();
Collections.addAll(intList, 10, 20, 30, 40, 50);
List<Integer> intList2 = new ArrayList<>();
Collections.addAll(intList2, 12, 22, 32, 42, 52);
List<Integer> intList3 = new ArrayList<>();
Collections.addAll(intList3, 13, 23, 33, 43, 53);
Single<List<Integer>> singleList = Single.just(intList);
Single<List<Integer>> singleList2 = Single.just(intList2);
Single<List<Integer>> singleList3 = Single.just(intList3);
List<Single<List<Integer>>> listOfSinglesOfListofInts = new ArrayList<>();
Collections.addAll(listOfSinglesOfListofInts, singleList, singleList2, singleList3);
Observable
//Iterate through List<Singles>
.fromIterable(listOfSinglesOfListofInts)
//Get each single, convert to Observable and iterate to get all the integers
.flatMap(
singleListNew -> singleListNew
.toObservable()
//Iterate through each integer inside a list and return it
.flatMapIterable(integers -> integers)
)
//Get all the integers from above and convert to one list
.toList()
//Result is List<Integer>
.subscribe(
result -> System.out.println("**** " + result.toString() + " ****"),
error -> new Throwable(error)
);
希望这可以帮助。
添加回答
举报