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
































