Skip to content

Commit 7f275f9

Browse files
author
dupirefr
committed
[BAEL-2932] Using Predicate.not to negate a method reference
1 parent 0825c88 commit 7f275f9

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.predicate.not;
2+
3+
import java.util.List;
4+
import java.util.function.Predicate;
5+
import java.util.stream.Collectors;
6+
7+
public class FindPeople {
8+
public static void main(String[] args) {
9+
List<Person> people = List.of(
10+
new Person(1),
11+
new Person(18),
12+
new Person(2)
13+
);
14+
15+
people.stream()
16+
.filter(Person::isAdult)
17+
.collect(Collectors.toList());
18+
19+
people.stream()
20+
.filter(person -> !person.isAdult())
21+
.collect(Collectors.toList());
22+
23+
people.stream()
24+
.filter(Person::isNotAdult)
25+
.collect(Collectors.toList());
26+
27+
people.stream()
28+
.filter(Predicate.not(Person::isAdult))
29+
.collect(Collectors.toList());
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.predicate.not;
2+
3+
public class Person {
4+
private static final int ADULT_AGE = 18;
5+
6+
private int age;
7+
8+
public Person(int age) {
9+
this.age = age;
10+
}
11+
12+
public boolean isAdult() {
13+
return age >= ADULT_AGE;
14+
}
15+
16+
public boolean isNotAdult() {
17+
return !isAdult();
18+
}
19+
}

0 commit comments

Comments
 (0)