[BAEL-2932] Using Predicate.not to negate a method reference

This commit is contained in:
dupirefr 2019-05-15 08:19:30 +02:00
parent 0825c88d7a
commit 7f275f9b38
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package com.baeldung.predicate.not;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class FindPeople {
public static void main(String[] args) {
List<Person> people = List.of(
new Person(1),
new Person(18),
new Person(2)
);
people.stream()
.filter(Person::isAdult)
.collect(Collectors.toList());
people.stream()
.filter(person -> !person.isAdult())
.collect(Collectors.toList());
people.stream()
.filter(Person::isNotAdult)
.collect(Collectors.toList());
people.stream()
.filter(Predicate.not(Person::isAdult))
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.predicate.not;
public class Person {
private static final int ADULT_AGE = 18;
private int age;
public Person(int age) {
this.age = age;
}
public boolean isAdult() {
return age >= ADULT_AGE;
}
public boolean isNotAdult() {
return !isAdult();
}
}