RxAndroid Operators Part2

By | March 29, 2017

In the previous tutorial RxAndroid Part 1 we have seen about basics of RxAndroid like creating observable , observer and subscription . In this tutorial we will see how to apply operators to observable for manipulating data emitted by observable . There are several types of operators available in Rxjava for transforming , filtering ,Combining ,  Mathematical ,Utility Conditional.

1. Transforming Observables

Map

Transform the items emitted by an Observable by applying a function to each of them .

Lets define a method which returns array of Integer .

private Integer[] getIntergerList() {
    return new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
}

 

Observable<Integer> observable = Observable.from(getIntergerList())
        .map(new Func1<Integer, Integer>() {
            @Override
            public Integer call(Integer integer) {
                return integer * integer;
            }
        });

 

mSubscription = observable.subscribe(new Action1<Integer>() {
    @Override
    public void call(Integer integer) {
        textView.append(String.valueOf(integer) + "\n");
    }
});

Output

Emitted Data : 1   2   3   4   5   6   7   8   9

Transformed Data : 1   4   9   16  25  36  49   64   81

Code:https://github.com/hdpavan/RxAndroidSample/blob/master/app/src/main/java/com/tutorialsbuzz/rxandroidsample/Examples/FragmentExample7.java

2. Filtering Observables

Filter

filter items emitted by an Observable

Observable<Integer> observable = Observable.from(getIntergerList())
        .filter(new Func1<Integer, Boolean>() {
            @Override
            public Boolean call(Integer integer) {
                if (integer % 2 == 0) {
                    return true;
                }
                return false;
            }
        });

 

mSubscription = observable.subscribe(new Action1<Integer>() {
    @Override
    public void call(Integer integer) {
        textView.append(String.valueOf(integer) + "\n");
    }
});

Output

Emitted Data : 1  2  3  4  5  6  7  8  9

Filtered Data : 2  4  6  8

Code:https://github.com/hdpavan/RxAndroidSample/blob/master/app/src/main/java/com/tutorialsbuzz/rxandroidsample/Examples/FragmentExample8.java

Take

Emit only the first n items emitted by an Observable

Observable<Integer> observable = 
                      Observable.from(getIntergerList()).take(5);

 

mSubscription = observable.subscribe(new Action1<Integer>() {
    @Override
    public void call(Integer integer) {
        textView.append(String.valueOf(integer) + "\n");

    }
});

Output 

Emitted Data : 1  2  3  4  5  6   7  8  9

Filtered Data : 1  2  3  4  5

Code:https://github.com/hdpavan/RxAndroidSample/blob/master/app/src/main/java/com/tutorialsbuzz/rxandroidsample/Examples/FragmentExample9.java

Skip

Ignore the first n items emitted by an Observable

Observable<Integer> observable = 
                     Observable.from(getIntergerList()).skip(5);

 

mSubscription = observable.subscribe(new Action1<Integer>() {
    @Override
    public void call(Integer integer) {
        textView.append(String.valueOf(integer) + "\n");

    }
});

Output 

Emitted Data : 1  2  3   4  5  6  7  8  9

Filtered Data : 6  7  8  9

Code:https://github.com/hdpavan/RxAndroidSample/blob/master/app/src/main/java/com/tutorialsbuzz/rxandroidsample/Examples/FragmentExample10.java

3. Combining Observables

Zip

Combine sets of items emitted by two or more Observables together via a specified function and emit items based on the results of this function .

Observer1

Observable<List<String>> observable1 = Observable
        .fromCallable(new Callable<List<String>>() {
            @Override
            public List<String> call() {
                return mRestClient.getCountryList();
            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());

Observer2

Observable<SampleLocation> observable2 = Observable
        .fromCallable(new Callable<SampleLocation>() {
            @Override
            public SampleLocation call() {
                return mRestClient.getUserLocation();
            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());

Combining Observer1 and Observer2

Observable<CombinedData> combine_observable =
        Observable.zip(observable1, observable2,
                new Func2<List<String>, SampleLocation, CombinedData>() {
                    @Override
                    public CombinedData call(List<String> strings, SampleLocation sampleLocation) {
                        CombinedData combinedData = new CombinedData();
                        combinedData.setStringList(strings);
                        combinedData.setSampleLocation(sampleLocation);

                        return combinedData;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

 

combine_observable.subscribe(new Observer<CombinedData>() {
    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
    }

    @Override
    public void onNext(CombinedData combinedData) {
        displayData(combinedData);
    }
});

Code:https://github.com/hdpavan/RxAndroidSample/blob/master/app/src/main/java/com/tutorialsbuzz/rxandroidsample/Examples/FragmentExample11.java