如何将两个可观察量的排放量收集到不同的列表中?使用 RxJava?

How to collect emissions of two observables into different lists? using RxJava?

我有两个可观察对象,每个对象都会 return 一个对象列表。我想收集这些列表,然后使用 Android 中的 DiffUtil 功能从第一个列表中删除不存在的项目。除了从第一个 observable 的 onComplete 发射另一个 observable 之外,有人对此有任何想法吗?或者这甚至可能吗?

Observable1 -> List1
Observable2 -> List2
DiffUtil(List1, List2)
   delete from List1 items that are non-existent in List2

只需使用 zip 运算符:

list1Observable.zipWith(list2Observable,
                (list1, list2) -> {
                //DiffUtil list1 and list2 and return the filtered list
                }
        );

对于两个列表,我会使用合并运算符

/**
 * Here we merge two list and we sort the list for every new item added into.
 * Shall return
 * <p>
 * [1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]
 */
@Test
public void testMergeLists() {
    Observable.merge(Observable.from(Arrays.asList(2, 1, 13, 11, 5)), Observable.from(Arrays.asList(10, 4, 12, 3, 14, 15)))
            .collect(ArrayList<Integer>::new, ArrayList::add)
            .doOnNext(Collections::sort)
            .subscribe(System.out::println);

}

您可以在此处查看更多示例https://github.com/politrons/reactive