liveBook · Manning

Filtering

As explained in The Art of Code

Data filtering is the selection or removal of records according to specified validity, quality, or uniqueness criteria.

Filtering selects elements that satisfy a predicate while leaving the remaining stream available for later transformations. In a functional pipeline, a condition such as “published within the last year” can precede mapping books to author names; execution remains lazy until a terminal operation requests the result. section 6.3.1 section 6.5

Filter before transforming

A readable stream can name the rule and then compose it with the transformation: books.stream().filter(isRecentlyPublished).map(Book::author).toList(). The predicate keeps recent books; map then converts each retained Book into an author name. Inline predicates are convenient when the condition remains visually simple. section 6.3.1

Keep filtering pipelines understandable by making each operation tell one part of the story.

  • Group consecutive filters serving one purpose behind a well-named predicate such as isValidUser. section 6.3.3
  • Split a long pipeline into stages with intermediate variables when each stage becomes easier to test, explain, or maintain. section 6.3.3
  • Prefer imperative code when exception handling or excessive chaining makes the functional version harder to read. section 6.5
Stream<String> authorNames = books.stream()
   .filter(isRecentlyPublished)
   .map(toAuthor);

Browse the complete filter-and-map pipeline from Book objects to author-name strings. section 6.3.1

How The Art of Code covers it

How deep it goes
Defines predicates, lazy intermediate operations, readability limits, method extraction, and staged pipelines without descending into implementation internals.
Examples
Uses recently published books, active users, signup dates, email domains, sanitization, sorting, distinct results, and result limits.
What it assumes
Assumes familiarity with collections and basic programming while defining predicates, lambdas, functional interfaces, and stream operations.
What you can do after
Apply it via stream pipelines by writing predicates, retaining matching elements, and composing subsequent transformations clearly.
How it connects
Connects filtering with predicates, lambdas, higher-order functions, map, terminal operations, laziness, and functional programming.

Also explained in

Compare how 33 books explain filtering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.