RxJava Operator - Concat Vs Merge

RxJava is the most important library which is popular among the Android developers. It makes our life easy.

We use RxJava for multithreading, managing background tasks and removing callback hells. We can solve so many complex use-cases with the help of the RxJava. It enables us to do complex things very simple. It provides us the power.

With great power comes great responsibility

RxJava has so many operators. In order to use them correctly, we must know about them. Here, we will discuss the Concat and the Merge Operators.

Concat

Concat emits the emissions from two or more Observables without interleaving them. It will maintain the order of the observables while emitting the items. It means that it will emit all the items of the first observable and then it will emit all the items of the second observable and so on.

Let's understand it clearly by an example.

final String[] listFirst = {"A1", "A2", "A3", "A4"};
final String[] listSecond = {"B1", "B2", "B3"};

final Observable<String> observableFirst = Observable.fromArray(listFirst);
final Observable<String> observableSecond = Observable.fromArray(listSecond);

Observable.concat(observableFirst, observableSecond)
        .subscribe(new Observer<String>() {

            @Override
            public void onSubscribe(Disposable d) {
                
            }

            @Override
            public void onNext(String value) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

As we are using Concat Operator, it will maintain the order and emit the values as A1, A2, A3, A4, B1, B2, B3.

Merge

Merge combines multiple Observables into one by merging their emissions. It will not maintain the order while emitting the items.

Let's understand it clearly by an example.

final String[] listFirst = {"A1", "A2", "A3", "A4"};
final String[] listSecond = {"B1", "B2", "B3"};

final Observable<String> observableFirst = Observable.fromArray(listFirst);
final Observable<String> observableSecond = Observable.fromArray(listSecond);

Observable.merge(observableFirst, observableSecond)
        .subscribe(new Observer<String>() {

            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String value) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

As we are using Merge Operator, it will not maintain the order and can emit the values in any order such as A1, B1, A2, A3, B2, B3, A4 or A1, A2, B1, B2, A3, A4, B3 or can be anything.

This is how we should use the Concat and the Merge operators in RxJava depending on our use-case.

Learn “How to implement caching using RxJava Operators”

Apply Now: MindOrks Android Coding BootCamp and Learn Advanced Android

That’s it for now. Happy learning :)