liveBook · Manning

Filtering

As explained in Spring in Action, Sixth Edition

A reactive filter is a Project Reactor operator that emits only source elements satisfying a predicate.

Filtering lets selected values continue while leaving others out. In Project Reactor, `filter()` takes a `Predicate`: a test applied to each value from a source `Flux`. The resulting `Flux` emits only values that pass. section 11.3.3

Keep names without spaces

Start with `Flux.just("Yellowstone", "Yosemite", "Grand Canyon", "Zion", "Grand Teton")`, then call `.filter(name -> !name.contains(" "))`. The resulting Flux emits `Yellowstone`, `Yosemite`, and `Zion`; the two names containing spaces do not pass the predicate. A `StepVerifier` test can check those three values and completion. section 11.3.3

Choose the operator that matches the rule. `filter()` handles a general predicate; `distinct()` removes values already emitted. `skip()` and `take()` can also serve as filters when the rule depends on an item count or a duration. section 11.3.3

A Spring Integration filter serves a related purpose in a different kind of flow: it allows or disallows whole messages before the next step. For an integer message, an `@Filter` method returning `number % 2 == 0` can pass even numbers from an input channel to an output channel. In a Java DSL integration flow, `.filter((p) -> p % 2 == 0)` expresses the same rule; more involved rules can use a `GenericSelector` instead of a lambda. section 10.2 section 10.2.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Works through predicate-based filtering and an integration-flow alternative, but does not develop filter implementation internals.
Examples
Uses park-name strings to test a Reactor filter and even integers to show Spring Integration annotation and Java DSL filters.
What it assumes
Assumes basic Java lambdas and Flux construction while introducing the predicate as the rule that decides which values pass.
What you can do after
Apply it via Reactor `filter()` to select Flux values, or configure a Spring Integration filter to pass selected messages.
How it connects
Connects filtering to Flux, predicates, distinct(), skip(), take(), Spring Integration channels, and GenericSelector.

Also explained in

Compare how 33 books explain filtering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.