GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Predicate.not() for negation | java.evolved
Streams
Beginner
Predicate.not() for negationUse Predicate.not() to negate method references cleanly instead of writing lambda wrappers. 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());
See a problem with this code? Let us know.
Why the modern way wins
👁
Cleaner negationNo need to wrap method references in lambdas just to negate them. 🔗
ComposableWorks with any Predicate, enabling clean predicate chains. 📖
Reads naturallyPredicate.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
Proof
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();
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
JDK 11+
→
Streams
Beginner
Stream takeWhile / dropWhileJava 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)
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. |