[英]Rxjava multiple observable List
I have two observable list of String. 我有两个可观察到的String列表。 I want to achieve something like this in rxjava using zip or any other operation.
我想使用zip或任何其他操作在rxjava中实现类似的功能。
If list1 has data and list2 doesn't have any data - consider the dataset of list1 如果list1有数据而list2没有任何数据-考虑list1的数据集
If list1 has no data and list2 has any data - consider the dataset of list2 如果list1没有数据,而list2有任何数据-考虑list2的数据集
If list1 and list2 both have data, then take the intersection of both list 如果list1和list2都有数据,则取两个list的交集
List1 has 0-n elements and list2 has 0-m elements. 列表1具有0-n个元素,列表2具有0-m个元素。
There's no official way to do this with RxJava. 使用RxJava尚无官方方法。 I would suggest using flatMap on one of the Observables and then filtering each element through the other Observable.
我建议在其中一个Observable上使用flatMap,然后通过另一个Observable过滤每个元素。 I can't test this right now, but it should work.
我目前无法测试,但应该可以。
Edit: Something like: 编辑:类似:
list1
.defaultIfEmpty(/*Observable with empty string, for instance (for the empty case)*/)
.flatMap { element ->
return list2.defaultIfEmpty(element).filter(x -> x == element);
}
.observeOn(...)
.subscribeOn(...)
.subscribe(...)
It has been months since I used RxJava, but something of this sort should work. 自从我使用RxJava已经有几个月了,但是这种东西应该可以工作。
You can use zip
to have both lists ready, then you need to chose based on their state: 您可以使用
zip
来准备好两个列表,然后需要根据它们的状态进行选择:
Single<List<Integer>> singleList1 = ...
Single<List<Integer>> singleList2 = ...
Single.zip(singleList1, singleList2, (list1, list2) -> {
if (list1.isEmpty()) {
return list2;
}
if (list2.isEmpty()) {
return list1;
}
Set<Integer> set = new HashSet<>(list1);
List<Integer> result = new ArrayList<>(list2.size());
for (Integer i : list2) {
if (set.contains(i)) {
result.add(i);
}
}
return result;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.