Java 11: Predicate.not for readable negation

Predicate.not negates a method reference so filters read naturally.

Code
List<String> words = List.of("", "hi", "  ", "world");
List<String> nonBlank = words.stream()
        .filter(Predicate.not(String::isBlank))
        .toList();
System.out.println(nonBlank);
Output
[hi, world]
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26