RxJava
In our examples, we will use RxJava 2.2.21 (http://reactivex.io) . It can be added to the project using the following dependency:
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.21</version>
</dependency>
First, let’s 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
, and5
. - Only filter the even numbers (that is,
2
and4
). - Calculate the 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 (see the ObservableIntro
class and the squareRootSum()
method):
double a = IntStream.rangeClosed(1, 5)
...