GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Predicate.not() for negation | java.evolved

Home / Streams / Predicate.not() for negation
Streams Beginner

Predicate.not() for negation

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

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

Functional Programming Streams API
Code Comparison
✕ 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))
    .collect(Collectors.toList());
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
👁

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
JDK Support
Predicate.not() for negation
Available

Available since JDK 11 (September 2018).

How it works

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.

Related Documentation
Predicate.not() ↗
Proof
View proof source ↗
Related patterns
Streams Beginner

Stream.toList()

Java 8
List<String> result = stream
    .filter(s -> s.length() > 3)
    .collect(Collectors.toList());
Java 16+
List<String> result = stream
    .filter(s -> s.length() > 3)
    .toList();
Hover to see modern ➜
JDK 16+ →
Strings Beginner

String.isBlank()

Java 8
boolean blank =
    str.trim().isEmpty();
// or: str.trim().length() == 0
Java 11+
boolean blank = str.isBlank();
// handles Unicode whitespace too
Hover to see modern ➜
JDK 11+ →
Streams Beginner

Stream takeWhile / dropWhile

Java 8
List<Integer> result = new ArrayList<>();
for (int n : sorted) {
    if (n >= 100) break;
    result.add(n);
}
// no stream equivalent in Java 8
Java 9+
var result = sorted.stream()
    .takeWhile(n -> n < 100)
    .toList();
// or: .dropWhile(n -> n < 10)
Hover to see modern ➜
JDK 9+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.