RxJava:如何使用 Observable 而不是嵌套循环?
RxJava: how to use Observable instead nested loops?
现在我使用带 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()
" return 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
以获得完整的配对组合?
您可以将需要执行的操作嵌套在 flatMap
中以实现此目的:
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 observableList
.flatMap(listElem -> observableOperation
.map(operationElem -> {
// some operations here
subject.onNext(unit);
return unit; // Assuming unit is a BaseUnit
})
);
}
我不太喜欢将 ReplaySubject
传递给您的方法。相反,您可以将您的方法作为来源并让您的 subject
订阅它:
private Observable<BaseUnit> getUnitObservables(ArrayList<Map<Integer, Integer>> list) {
return Observable.fromIterable(list)
.flatMap(listElem -> Observable.fromArray(MapOperationName.values())
.map(operationElem -> {
// some operations here
return new BaseUnit();
})
);
}
...
...
Subject<BaseUnit> subject = ReplaySubject.create();
subject
.mergeWith(getUnitObservables(listParam))
.subscribe();
现在我使用带 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()
" return 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
以获得完整的配对组合?
您可以将需要执行的操作嵌套在 flatMap
中以实现此目的:
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 observableList
.flatMap(listElem -> observableOperation
.map(operationElem -> {
// some operations here
subject.onNext(unit);
return unit; // Assuming unit is a BaseUnit
})
);
}
我不太喜欢将 ReplaySubject
传递给您的方法。相反,您可以将您的方法作为来源并让您的 subject
订阅它:
private Observable<BaseUnit> getUnitObservables(ArrayList<Map<Integer, Integer>> list) {
return Observable.fromIterable(list)
.flatMap(listElem -> Observable.fromArray(MapOperationName.values())
.map(operationElem -> {
// some operations here
return new BaseUnit();
})
);
}
...
...
Subject<BaseUnit> subject = ReplaySubject.create();
subject
.mergeWith(getUnitObservables(listParam))
.subscribe();