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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.