现在我使用嵌套循环ReplaySubject
来获取多个可观察对象,这需要在不使用任何循环的情况下进行转换,只使用可观察对象。
private Observable<BaseUnit> getUnitObservables(ArrayList<Map<Integer, Integer>> list, ReplaySubject<BaseUnit> subject) {
ArrayList<Observable<BaseUnit>> observables = new ArrayList<>();
for (Map<Integer, Integer> elem : list) {
for (MapOperationName operationName : MapOperationName.values()) { // MapOperationName type is enum
Observable<BaseUnit> observable = Observable.fromCallable(() -> {
// some operations here
subject.onNext(unit);
observables.add(observable);
});
}
}
return Observable.merge(observables);
}
" Map<Integer, Integer> elem : list
" 有 3 个元素。" MapOperationName.values()
" 返回 7 个元素。所以我在上面的例子中完全应该是 21 岁。
试图重构这样的东西,但只得到 3 个元素:
private Observable<BaseUnit> getUnitObservables(ArrayList<Map<Integer, Integer>> list, ReplaySubject<BaseUnit> subject) {
Observable<ListOperationName> observableOperation =
Observable.fromArray(MapOperationName.values());
Observable<List<Integer>> observableList = Observable.fromIterable(list);
return Observable.zip(observableList, observableOperation, (listElem, operationElem) -> {
subject.onNext(unit);
//some operations here
});
}
如何正确组合ArrayList
并Enum
获得全套配对组合?