RxJava Tutorial on RxJava Combining Operators

following are the operators which are used to create a single observable from multiple observables.

sr.no. operator & description
1 and/then/when

combine item sets using pattern and plan intermediaries.

2 combinelatest

combine the latest item emitted by each observable via a specified function and emit resulted item.

3 join

combine items emitted by two observables if emitted during time-frame of second observable emitted item.

4 merge

combines the items emitted of observables.

5 startwith

emit a specified sequence of items before starting to emit the items from the source observable

6 switch

emits the most recent items emitted by observables.

7 zip

combines items of observables based on function and emits the resulted items.

combining operator example

create the following java program using any editor of your choice in, say, c:\> rxjava.

observabletester.java

import io.reactivex.observable;
//using combinelatest operator to combine observables
public class observabletester {
   public static void main(string[] args) {    
      integer[] numbers = { 1, 2, 3, 4, 5, 6};
      string[] letters = {"a", "b", "c", "d", "e", "f", "g"};
      final stringbuilder result = new stringbuilder();
      observable<string> observable1 = observable.fromarray(letters);
      observable<integer> observable2 = observable.fromarray(numbers);
      observable.combinelatest(observable1, observable2, (a,b) -> a + b)
         .subscribe( letter -> result.append(letter));
      system.out.println(result);
   }
}

verify the result

compile the class using javac compiler as follows −

c:\rxjava>javac observabletester.java

now run the observabletester as follows −

c:\rxjava>java observabletester

it should produce the following output −

g1g2g3g4g5g6