tutorial, concat, rx,

Rx - concat

Upendra Upendra Follow Jan 23, 2025 · 2 mins read
Rx - concat
Share this

Concat vs Merge

Concat

Emit the emissions from two or more Observables without interleaving them.

The Concat operator concatenates the output of multiple Observables so that they act like a single Observable, with all of the items emitted by the first Observable being emitted before any of the items emitted by the second Observable (and so forth, if there are more than two).

Concat waits to subscribe to each additional Observable that you pass to it until the previous Observable completes. Note that because of this, if you try to concatenate a “hot” Observable, that is, one that begins emitting items immediately and before it is subscribed to, Concat will not see, and therefore will not emit, any items that Observable emits before all previous Observables complete and Concat subscribes to the “hot” Observable.

String[] firstSource = { "first1", "first2", "first3", "first4" };
String[] secondSource = { "second1", "second2", "second3", "second4" };

Observable<String> firstObservable = Observable.fromArray(firstSource);
Observable<String> secondObservable = Observable.fromArray(secondSource);

Observable.concat(firstObservable, secondObservable)
        .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() {

            }
        });

Output:

first1, first2, first3, first4, second1, second2, second3, second4 

Merge

Combine multiple Observables into one by merging their emissions

Merge combine the output of multiple Observables so that they act like a single Observable, but it won’t maintain the sequential execution.

String[] firstSource = { "first1", "first2", "first3", "first4" };
String[] secondSource = { "second1", "second2", "second3", "second4" };

Observable<String> firstObservable = Observable.fromArray(firstSource);
Observable<String> secondObservable = Observable.fromArray(secondSource);

Observable.concat(firstObservable, secondObservable)
        .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() {

            }
        });

Output (may be in any order):

first1, second`, first2, second2, first3, second3, first4, second4

http://reactivex.io/documentation/operators/concat.html
http://reactivex.io/documentation/operators/merge.html
https://www.androidhive.info/RxJava/rxjava-operators-concat-merge/

credit goes to @swayangjit
Join Newsletter
Get the latest news right in your inbox. We never spam!
Upendra
Written by Upendra Follow
Hi, I am Upendra, the author in Human and machine languages,I don't know to how 3 liner bio works so just Connect with me on social sites you will get to know me better.