• Home
  • About
    • Thoughts To Pen photo

      Thoughts To Pen

      My thoughts on Computer Programming || Psychology || Personal Finances || & much more...

    • Learn More
    • Twitter
    • Instagram
    • Github
    • StackOverflow
  • Posts
    • All Posts
    • All Tags
  • Projects
  • Portfolio
  • Resources
  • About

Predicate::not and More Enhancements (Java 11)

05 Dec 2025

While the new HTTP Client and String utilities get most of the spotlight, Java 11 also included several “hidden gems” that significantly improve code readability and the developer experience. Let’s look at three such improvements that you’ll likely use every day.

1. Predicate.not()

Writing clean filter conditions in Streams sometimes felt clunky when you wanted to negate a condition.

The Old Way: You had to use a lambda or a complex method reference.

list.stream()
    .filter(s -> !s.isBlank()) // Lambda with negation
    .collect(Collectors.toList());

The Java 11 Way: The Predicate.not() static method allows you to negate a method reference, making the code read like a natural sentence.

import java.util.function.Predicate;

list.stream()
    .filter(Predicate.not(String::isBlank)) // Reads: "Filter NOT String is blank"
    .collect(Collectors.toList());

2. Simplified Collection to Array

Converting a List to an Array has historically been a bit awkward. You either had to provide an empty array of the right type or a correctly sized one.

Before Java 11:

String[] array = list.toArray(new String[0]);

Java 11 Enhancement: The Collection interface now has an overloaded toArray method that takes an IntFunction (a constructor reference), which is much cleaner.

String[] array = list.toArray(String[]::new);

3. Running Single-File Source Code

One of the most surprising features of Java 11 is the ability to run a Java source file directly without compiling it first with javac. This is perfect for small scripts or beginners learning the language.

The Old Way:

$ javac HelloWorld.java
$ java HelloWorld

The Java 11 Way:

$ java HelloWorld.java

The JVM compiles the file in memory and executes it immediately. It doesn’t even produce a .class file on your disk!

4. Nest-Based Access Control (Internal API)

While this is mostly “under the hood,” it’s good to know. Java 11 improved how nested classes (like inner classes) access each other’s private members. Before Java 11, the compiler had to generate “bridge methods” to allow this access. Now, the JVM has a native concept of “Nests,” making the generated bytecode smaller and the access more efficient.

Real-World Example: A Quick Script

Imagine you want to write a quick script to find all non-empty files in a directory and get them as an array.

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Predicate;

public class CleanupScript {
    public static void main(String[] args) throws Exception {
        Path[] files = Files.list(Path.of("."))
                .filter(Predicate.not(Files::isDirectory))
                .toArray(Path[]::new);
        
        System.out.println("Found " + files.length + " files.");
    }
}

You can save this as CleanupScript.java and run it instantly with java CleanupScript.java.

Conclusion

Java 11 proving that the “little things” matter. Predicate.not() makes our functional code more expressive, toArray(String[]::new) brings consistency, and direct source execution makes Java feel as nimble as a scripting language. These features collectively make Java 11 a much more modern and pleasant environment to work in.



programmingjavajava11 Share Tweet Msg