Java 11 introduced new method to Predicate interface as not() to negate an existing predicate similar to negate method.
Consider the following program −
Java11NotPredicate.java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Java11NotPredicate {
public static void main(String[] args) {
List<String> tutorialsList = Arrays.asList("Ela", "", " ", "Java", "\n", "\t");
// 1. Filter non blank values using predicate
List<String> tutorials = tutorialsList.stream()
.filter(Predicate.not(String::isBlank))
.collect(Collectors.toList());
// 2. Iterate filter values
tutorials.forEach(tutorial -> System.out.println(tutorial));
}
}
Output
Ela
Java