Predicate.not() for negation
Use Predicate.not() to negate method references cleanly instead of writing lambda wrappers.
Porównanie kodu
✕ 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();
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
Lambda negation
Nowoczesne podejście
Predicate.not()
Od JDK
11
Poziom trudności
Początkujący
Wsparcie JDK
Predicate.not() for negation
Dostępne
Available since JDK 11 (September 2018).
Jak to działa
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.
Powiązana dokumentacja