adam bien's blog

How to negate a filter / the "not" 📎

To negate a Stream::filter use the Java 11+ method Predicate::not:

import java.util.List;
import static java.util.function.Predicate.*;

var languages = List.of("java", "c", "c++", "javascript");
var nonJVM = languages.stream().filter(not(l -> l.equalsIgnoreCase("java"))).toList();
System.out.println(nonJVM);

The code generates the following output:

[c, c++, javascript]