Observable.merge() and mergeWith()

The Observable.merge() operator will take two or more Observable<T> sources emitting the same type T and then consolidate them into a single Observable<T>.

If we have only two to four Observable<T> sources to merge, you can pass each one as an argument to the Observable.merge() factory. In the following code snippet, I have merged two Observable<String> instances into one Observable<String>:

import io.reactivex.Observable;public class Launcher {      public static void main(String[] args) {        Observable<String> source1 =          Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon");        Observable<String> source2 =          Observable.just("Zeta", "Eta", "Theta");        Observable.merge(source1, source2)          .subscribe(i -> System.

Get Learning RxJava now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.