RxJava Tutorial on RxJava Filtering Operators

following are the operators which are used to selectively emit item(s) from an observable.

sr.no. operator & description
1

debounce

emits items only when timeout occurs without emiting another item.

2

distinct

emits only unique items.

3

elementat

emit only item at n index emitted by an observable.

4

filter

emits only those items which pass the given predicate function.

5

first

emits the first item or first item which passed the given criteria.

6

ignoreelements

do not emits any items from observable but marks completion.

7

last

emits the last element from observable.

8

sample

emits the most recent item with given time interval.

9

skip

skips the first n items from an observable.

10

skiplast

skips the last n items from an observable.

11

take

takes the first n items from an observable.

12

takelast

takes the last n items from an observable.

filtering operator example

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

observabletester.java

import io.reactivex.observable;
//using take operator to filter 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
         .take(2)
         .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 −

ab