Search code examples
androidrx-javarx-java2reactivex

Spilt one object observable into two by extracting with custom map


I have List of object List data as observable which includes two child List , List. I want to extract and process them separately.

    CustomMapper mapper = new CustomMapper(parentList).

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

Solution

  • You can achieve similar result with share() Operator

     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));
    

    result is:

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

    More info : https://medium.com/mindorks/how-to-use-rxjava-share-operator-26b08973771a