00:00

Optional.isEmpty() in Java 11

Last updated: · Standard (Java 11)

Optional.isEmpty() returns true when an Optional holds no value. It was added in Java 11 as the direct opposite of isPresent(), so code no longer needs to negate a method call.

This tutorial shows where isEmpty() improves readability, especially in streams, and how it fits with the other Optional methods added after Java 8.

1. Why Was isEmpty() Added?

Checking for a missing value with !opt.isPresent() is easy to misread, because the ! is small and far from the method name. It also cannot be written as a method reference. isEmpty() fixes both problems and matches the naming of String.isEmpty() and Collection.isEmpty().

2. Syntax

Syntax.java
public boolean isEmpty()   // true if no value is present

Optional<String> name = Optional.empty();
name.isEmpty();     // true
name.isPresent();   // false

3. Practical Examples

Example 1: A clearer "not found" check

IsEmptyDemo.java
import java.util.Map;
import java.util.Optional;

public class IsEmptyDemo {
    static final Map<String, String> COUPONS = Map.of("WELCOME10", "10% off");

    static Optional<String> findCoupon(String code) {
        return Optional.ofNullable(COUPONS.get(code));
    }

    public static void main(String[] args) {
        for (String code : new String[] { "WELCOME10", "FREESHIP" }) {
            Optional<String> coupon = findCoupon(code);

            // Java 8 style: a negated isPresent()
            if (!coupon.isPresent()) {
                System.out.println(code + ": (old check) not found");
            }
            // Java 11: isEmpty() reads naturally
            if (coupon.isEmpty()) {
                System.out.println(code + ": invalid coupon");
            } else {
                System.out.println(code + ": " + coupon.get());
            }
        }
    }
}
Output
WELCOME10: 10% off
FREESHIP: (old check) not found
FREESHIP: invalid coupon

Example 2: As a method reference in streams

Optional::isEmpty can be passed directly to filter. Java 11 also added Predicate.not, which negates a method reference.

IsEmptyInStreams.java
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class IsEmptyInStreams {
    public static void main(String[] args) {
        List<Optional<String>> emails = List.of(
                Optional.of("asha@mail.com"), Optional.empty(),
                Optional.of("ravi@mail.com"), Optional.empty());

        // isEmpty works as a method reference, no lambda with ! needed
        long missing = emails.stream().filter(Optional::isEmpty).count();
        System.out.println("Customers without email: " + missing);

        List<String> present = emails.stream()
                .filter(Predicate.not(Optional::isEmpty))     // Predicate.not is also Java 11
                .map(Optional::get)
                .collect(Collectors.toList());
        System.out.println("Emails: " + present);
    }
}
Output
Customers without email: 2
Emails: [asha@mail.com, ravi@mail.com]

4. Optional Improvements After Java 8

VersionMethodPurpose
Java 9ifPresentOrElse, or, stream()handle both cases; supply another Optional; convert to a stream
Java 10orElseThrow()same as get() but with a clearer name
Java 11isEmpty()the opposite of isPresent()
isPresent and isEmpty are opposites For an Optional containing a value, isPresent is true and isEmpty is false. For Optional.empty, isPresent is false and isEmpty is true. Optional.of("10% off") value present isPresent() = true isEmpty() = false Optional.empty() no value isPresent() = false isEmpty() = true
Figure 1: isEmpty() always returns the opposite of isPresent().

5. Important Points and Best Practices

  • Use isEmpty() for "missing" checks and isPresent() for "found" checks; avoid negations.
  • Often you do not need either: orElse, map and ifPresentOrElse express the intent more directly.
  • Calling get() after an isEmpty() check of false is safe, but orElseThrow() is clearer.

6. Summary

  • Optional.isEmpty() was added in Java 11.
  • It returns true when no value is present, the opposite of isPresent().
  • It reads better than !isPresent() and works as a method reference.
  • Java 9 to 11 added several other helpful Optional methods.

Related Tutorials

7. References