liveBook · Manning

Filtering

As explained in 100 Java Mistakes and How to Avoid Them

Filtering is the operation of selecting or excluding items from data or a collection according to specified criteria.

Filtering retains stream elements whose predicate returns true. In a Java Stream API pipeline, filter() is an intermediate operation: it records work, but processing begins only when a terminal operation runs. section 9.2

Avoid these narrowly scoped filtering mistakes:

  • Do not use filter() predicates for unrelated mutations. A predicate that adds elements to another collection depends on call count and order, and may fail or lose data under parallelStream(). section 9.2
  • Do not assume every terminal operation or collector accepts null values. Filter nulls early with filter(Objects::nonNull), or verify the specific operation in its documentation or JShell. section 9.4
  • Use partitioningBy() when the goal is to retain both predicate outcomes instead of smuggling the rejected elements into a side-effect collection. section 9.2

Partition instead of mutating inside filter()

Avoid: list.stream().filter(str -> { if (str.contains("needle")) return true; containNoNeedle.add(str); return false; }).toList(); Prefer: Map<Boolean, List<String>> partitions = list.stream().collect(Collectors.partitioningBy(str -> str.contains("needle"))); section 9.2

Map<Boolean, List<String>> partitions = list.stream()
  .collect(Collectors.partitioningBy(str -> str.contains("needle")));
List<String> containNoNeedle = partitions.get(false);
List<String> containNeedle = partitions.get(true);

Shows the canonical partitioningBy() arrangement for collecting matching and nonmatching elements. section 9.2

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains lazy execution, predicate side effects, null handling, and safer alternatives through focused Java examples.
Examples
Uses null removal, side-effecting predicates, partitioningBy(), and trimmed strings limited after filtering.
What it assumes
Assumes familiarity with Java streams, predicates, collectors, and terminal operations.
What you can do after
Apply filtering via Java Stream API, remove nulls, partition matches, and test uncertain terminal behavior in JShell.
How it connects
Connects filter() with Stream API, terminal operations, collectors, partitioningBy(), peek(), and Optional.

Also explained in

Compare how 33 books explain filtering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.