Streams Początkujący

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();
Widzisz problem z tym kodem? Daj nam znać.
👁

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
Predicate.not() for negation
Dostępne

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.

Udostępnij 𝕏 🦋 in