Zipping allows you to take an emission from each Observable
source and combine it into a single emission. Each Observable
can emit a different type, but you can combine these different emitted types into a single emission. Here is an example, If we have an Observable<String>
and an Observable<Integer>
, we can zip each String
and Integer
together in a one-to-one pairing and concatenate it with a lambda:
import io.reactivex.Observable; public class Launcher { public static void main(String[] args) { Observable<String> source1 = Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon"); Observable<Integer> source2 = Observable.range(1,6); Observable.zip(source1, source2, (s,i) -> s + "-" + i) .subscribe(System.out::println); } }
The output is as follows:
Alpha-1 Beta-2 Gamma-3 Delta-4 Epsilon-5
The zip()
function received both Alpha
and a 1
and then paired them up into a concatenated string...