
1. Overview
In this article, we will learn the RxJava zip operator with examples. To learn more about other RxJava features, refer to these articles.
2. RxJava zip operator
The RxJava zip operator combines the emissions of multiple Observables together via a specified function and emits single items for each combination based on the results of the provided function.
The?Zip?method returns an Observable that applies a function of your choosing to the combination of items emitted, in sequence, by two (or more) other Observables. It can take up to a maximum of 9 observables as input.
The following is the zip overloaded method that takes up to 9 observable sources as input.
public static <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> Observable<R> zip(
@NonNull ObservableSource<? extends T1> source1,
@NonNull ObservableSource<? extends T2> source2,
@NonNull ObservableSource<? extends T3> source3,
@NonNull ObservableSource<? extends T4> source4,
@NonNull ObservableSource<? extends T5> source5,
@NonNull ObservableSource<? extends T6> source6,
@NonNull ObservableSource<? extends T7> source7,
@NonNull ObservableSource<? extends T8> source8,
@NonNull ObservableSource<? extends T9> source9,
@NonNull Function9<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? super T9, ? extends R> zipper) The results of the provided function becomes the items emitted by the returned Observable.
It applies this function in strict sequence, so the first item emitted by the new Observable will result from the function applied to the first item emitted by Observable #1 and the first item emitted by Observable #2.
Similarly, the second item emitted by the new zip-Observable will result from the function applied to the second item emitted by Observable #1 and the second item emitted by Observable #2; and so forth.
It will only emit as many items as the number of items emitted by the source Observable that emits the fewest items.
2.1. RxJava zip operator – input functions
As mentioned earlier, the zip takes a function which would combine the items emitted by the source observables.
Based on the overloaded zip method, the type of the function varies. For example, the following zip function takes two observable sources and BiFunction as input.
public static <T1, T2, R> Observable<R> zip( @NonNull ObservableSource<? extends T1> source1, @NonNull ObservableSource<? extends T2> source2, @NonNull BiFunction<? super T1, ? super T2, ? extends R> zipper)The following
zipoverloaded function that takes three observables andFunction3as input.public static <T1, T2, T3, R> Observable<R> zip( @NonNull ObservableSource<? extends T1> source1, @NonNull ObservableSource<? extends T2> source2, @NonNull ObservableSource<? extends T3> source3, @NonNull Function3<? super T1, ? super T2, ? super T3, ? extends R> zipper)2.2. RxJava Zip Example
Let’s see RxJava Zip examples to understand the above concepts.
The following test case contains two source observables
observableOneandobservableTwo. ThezipperBiFunctiontakes the items emitted by source observables and combine them i.e., this function takes the first item emitted byobservableOneandobservableTwoas input and emits the first item of newObservable.Similarly, the second item emitted by the new zip-
Observablewill result from the function applied to the second item emitted byobservableOneandobservableTwo; and so forth.@Test public void testZipOperator() { Observable<String> observableOne = Observable.just("Hello", "World"); Observable<String> observableTwo = Observable.just("Bye", "Friends"); BiFunction<String, String, String> zipper = new BiFunction<String, String, String>() { @Override public String apply(String s, String s2) { return s + "|" + s2; } }; Observable<String> resultObservable = Observable.zip(observableOne, observableTwo, zipper); resultObservable.subscribe(item -> System.out.println("Emitted item : " + item), error -> error.printStackTrace(), () -> System.out.println("Done")); }If you execute the above test case, it produces the following output.
Emitted item : Hello|Bye Emitted item : World|Friends DoneLet’s take another
zipexample with varying sizes. It will only emit as many items as the number of items emitted by the sourceObservablethat emits the fewest items.The following
observableTwocontains fewer items than theobservableOne. So the resultantObservableemits only as many items as inobservableTwo.@Test public void testZipOperator() { Observable<String> observableOne = Observable.just("1", "2", "3", "4", "5", "6"); Observable<String> observableTwo = Observable.just("A", "B"); BiFunction<String, String, String> zipper = new BiFunction<String, String, String>() { @Override public String apply(String s, String s2) { return s + "|" + s2; } }; Observable<String> resultObservable = Observable.zip(observableOne, observableTwo, zipper); resultObservable.subscribe(item -> System.out.println("Emitted item : " + item), error -> error.printStackTrace(), () -> System.out.println("Done")); }Since
observableTwocontains fewer items (2), the above code emits only two items as output:Emitted item : 1|A Emitted item : 2|B Done3. Conclusion
To sum up, we have learned the RxJava zip operator with a few examples. You can find code samples in our GitHub repository.