The filter() operator

The filter() operator enables selection of the data on the passed condition. The API takes a Boolean predicate, which is evaluated for every emitted value, in order to determine whether it is selected. Filtering is quite common. Let's suppose that we want to select dates based on a month range, or we want to select employee data based on employee IDs. In those cases, the Boolean predicate passed to the filter holds the selection logic. This can be quite flexible, and can be adapted to different needs.

Let's extend our Fibonacci generator to only select even numbers, as follows:

fibonacciGenerator.filter(a -> a%2 == 0).subscribe(t -> {
    System.out.println(t);
});

In the preceding code, the predicate performs a divisibility ...

Get Hands-On Reactive Programming with Reactor now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.