The filter
stream operation appears on several places in Chapter 2 and Chapter 3, so it’s difficult to say where to put this, but there should be an example of “filtering by a negated predicate”, maybe on page 60:
The code presented:
Files.list(Paths.get("."))
.filter(Files::isDirectory)
.forEach(System.out::println);
But what if we want to have everything EXCEPT directories while using the method reference?
This would of course work:
Files.list(Paths.get("."))
.filter(file -> !Files.isDirectory(file))
.forEach(System.out::println);
But nicer is this application of “predicate composition”
Files.list(Paths.get("."))
.filter(Predicate.not(file -> Files.isDirectory(file)))
.forEach(System.out::println);
which simplifies to
Files.list(Paths.get("."))
.filter(Predicate.not(Files::isDirectory))
.forEach(System.out::println);