通过使用自定义地图提取将一个可观察对象拆分为两个

Spilt one object observable into two by extracting with custom map

我有 List of object List 数据作为可观察的,其中包括两个子 List List。我想单独提取和处理它们。

    CustomMapper mapper = new CustomMapper(parentList).

    object1List=mapper.getObject1List();
    object2List=mapper.getObject2List();
    object1Observable.subscribe();
    object2Observable.subscribe();

您可以使用 share() 运算符获得类似的结果

 Observable o = Observable.just(1,2,"one","two",3,"four").share();
        o.filter(item -> item instanceof String)
              .map((Function<Object, String>) o1 -> o1.toString())
                .subscribe(o1 -> System.out.println("String is "+o1));

        o.filter(item -> item instanceof Integer)
                .map((Function<Object, Integer>) o1 -> ((Integer) o1))
                .subscribe(o1 -> System.out.println("Integer is "+o1));

结果是:

String is one
String is two
String is four
Integer is 1
Integer is 2
Integer is 3

更多信息:https://medium.com/mindorks/how-to-use-rxjava-share-operator-26b08973771a