Optional.isEmpty() in 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
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
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());
}
}
}
}
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.
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);
}
}
Customers without email: 2 Emails: [asha@mail.com, ravi@mail.com]
4. Optional Improvements After Java 8
| Version | Method | Purpose |
|---|---|---|
| Java 9 | ifPresentOrElse, or, stream() | handle both cases; supply another Optional; convert to a stream |
| Java 10 | orElseThrow() | same as get() but with a clearer name |
| Java 11 | isEmpty() | the opposite of isPresent() |
5. Important Points and Best Practices
- Use
isEmpty()for "missing" checks andisPresent()for "found" checks; avoid negations. - Often you do not need either:
orElse,mapandifPresentOrElseexpress the intent more directly. - Calling
get()after anisEmpty()check offalseis safe, butorElseThrow()is clearer.
6. Summary
Optional.isEmpty()was added in Java 11.- It returns
truewhen no value is present, the opposite ofisPresent(). - It reads better than
!isPresent()and works as a method reference. - Java 9 to 11 added several other helpful
Optionalmethods.
Related Tutorials
7. References
Frequently Asked Questions (FAQ)
Q1. What does Optional.isEmpty() do?
It returns true if the Optional contains no value and false otherwise.
Q2. Which Java version added Optional.isEmpty()?
Java 11.
Q3. Is isEmpty() the same as !isPresent()?
Yes, the result is always the opposite of isPresent(). isEmpty() simply reads more clearly and can be used as a method reference.
Q4. What other Optional methods were added after Java 8?
Java 9 added ifPresentOrElse, or and stream; Java 10 added the no-argument orElseThrow(); Java 11 added isEmpty().