Java 11 (LTS) · Topic 4 of 6
Predicate.not
Definition​
Predicate.not (Java 11) reverses a condition so you can use it with a method reference, for example filter(Predicate.not(String::isBlank)) instead of writing a lambda with !.
Example code​
Keeping only the non-blank lines
import java.util.List;
import java.util.function.Predicate;
class Demo {
public static void main(String[] args) {
List<String> lines = List.of("first", " ", "second", "");
List<String> filled = lines.stream()
.filter(Predicate.not(String::isBlank)) // reads like plain English
.toList();
System.out.println(filled);
}
}
// Output:
// [first, second]
Finished this page? Track your progress (saved only in your browser).