-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpredicate-not.yaml
More file actions
47 lines (47 loc) · 1.58 KB
/
Copy pathpredicate-not.yaml
File metadata and controls
47 lines (47 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
id: 87
slug: "predicate-not"
title: "Predicate.not() for negation"
category: "streams"
difficulty: "beginner"
jdkVersion: "11"
oldLabel: "Java 8"
modernLabel: "Java 11+"
oldApproach: "Lambda negation"
modernApproach: "Predicate.not()"
oldCode: |-
List<String> nonEmpty = list.stream()
.filter(s -> !s.isBlank())
.collect(Collectors.toList());
modernCode: |-
List<String> nonEmpty = list.stream()
.filter(Predicate.not(String::isBlank))
.toList();
summary: "Use Predicate.not() to negate method references cleanly instead of writing\
\ lambda wrappers."
explanation: "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."
whyModernWins:
- icon: "👁"
title: "Cleaner negation"
desc: "No need to wrap method references in lambdas just to negate them."
- icon: "🔗"
title: "Composable"
desc: "Works with any Predicate, enabling clean predicate chains."
- icon: "📖"
title: "Reads naturally"
desc: "Predicate.not(String::isBlank) reads like English."
support:
state: "available"
description: "Available since JDK 11 (September 2018)."
prev: "streams/optional-or"
next: "concurrency/virtual-threads"
related:
- "streams/stream-tolist"
- "strings/string-isblank"
- "streams/stream-takewhile-dropwhile"
docs:
- title: "Predicate.not()"
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/function/Predicate.html#not(java.util.function.Predicate)"