Streams Beginner

Use Predicate.not() to negate method references cleanly instead of writing lambda wrappers.

โœ• Java 8
List<String> nonEmpty = list.stream()
    .filter(s -> !s.isBlank())
    .collect(Collectors.toList());
โœ“ Java 11+
List<String> nonEmpty = list.stream()
    .filter(Predicate.not(String::isBlank))
    .toList();
See a problem with this code? Let us know.
๐Ÿ‘

Cleaner negation

No need to wrap method references in lambdas just to negate them.

๐Ÿ”—

Composable

Works with any Predicate, enabling clean predicate chains.

๐Ÿ“–

Reads naturally

Predicate.not(String::isBlank) reads like English.

Old Approach
Lambda negation
Modern Approach
Predicate.not()
Since JDK
11
Difficulty
Beginner
Predicate.not() for negation
Available

Available since JDK 11 (September 2018).

Before Java 11, negating a method reference required wrapping it in a lambda. Predicate.not() lets you negate any predicate directly, keeping the code readable and consistent with method reference style throughout the stream pipeline.

Share ๐• ๐Ÿฆ‹ in โฌก