RxJava Tutorial on RxJava Creating Operators

following are the operators which are used to create an observable.

sr.no. operator & description
1

create

creates an observable from scratch and allows observer method to call programmatically.

2

defer

do not create an observable until an observer subscribes. creates a fresh observable for each observer.

3

empty/never/throw

creates an observable with limited behavior.

4

from

converts an object/data structure into an observable.

5

interval

creates an observable emitting integers in sequence with a gap of specified time interval.

6

just

converts an object/data structure into an observable to emit the same or same type of objects.

7

range

creates an observable emitting integers in sequence of given range.

8

repeat

creates an observable emitting integers in sequence repeatedly.

9

start

creates an observable to emit the return value of a function.

10

timer

creates an observable to emit a single item after given delay.

creating operator example

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

observabletester.java

import io.reactivex.observable;
//using fromarray operator to create an observable
public class observabletester  {
   public static void main(string[] args) { 
      string[] letters = {"a", "b", "c", "d", "e", "f", "g"};
      final stringbuilder result = new stringbuilder();
      observable<string> observable = observable.fromarray(letters);
      observable
         .map(string::touppercase)
         .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 −

abcdefg