RxJava - Mathematical Operators
Following are the operators which operates on entire items emitted by an Observable.
| Sr.No. | Operator & Description |
|---|---|
| 1 |
concat Emits all items from multiple Observable without interleaving. |
| 2 |
count Counts all items and emit the result. |
| 3 |
reduce Apply a function on each item and return the result. |
| 4 |
sum Evaluates sum of all items and emit the result. |
Example - Usage of count operator
ObservableTester.java
package com.tutorialspoint;
import io.reactivex.rxjava3.core.Observable;
//Using count operator to operate on an Observable
public class ObservableTester {
public static void main(String[] args) {
String[] letters = {"a", "b", "c", "d", "e", "f", "g"};
Observable.fromArray(letters)
.count()
.subscribe(System.out::println);
}
}
Output
Compile and Run the code to verify the following output −
7
Example - Usage of reduce operator
ObservableTester.java
package com.tutorialspoint;
import io.reactivex.rxjava3.core.Observable;
//Using reduce operator to get sum of numbers
public class ObservableTester {
public static void main(String[] args) {
Observable.just(1,2,3,4,5,6,7)
.reduce(0, (a,b) -> a + b)
.subscribe(s -> System.out.println("Sum: " + s));
}
}
Output
Compile and Run the code to verify the following output −
28
Advertisements