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]

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments
License