We will use RxJava 2.2.7 (http://reactivex.io) in our examples. It can be added to the project by the following dependency:
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.7</version>
</dependency>
Let's first compare two implementations of the same functionality using the java.util.stream package and the io.reactivex package. The sample program is going to be very simple:
- Create a stream of integers 1,2,3,4,5.
- Filter only even numbers (2 and 4).
- Calculate a square root of each of the filtered numbers.
- Calculate the sum of all the square roots.
Here is how it can be implemented using the java.util.stream package:
double a = IntStream.rangeClosed(1, 5)
.filter(i -> i % 2 == 0)
.mapToDouble(Double::valueOf)
...