Optional is a container that either holds a value or holds nothing. It exists to make "there might be no result" a part of a method's signature instead of something that you discover later from a null reference. Used well it removes a whole family of the null checks. Used badly it just moves the null problem one level down.
The previous lesson in this module covered "What Are Functional Interfaces and Method References?", and this lesson is the last one in the Generics and Lambdas module.
What problem is Optional solving?
A method that may not find anything has two choices today. It can return null, or it can return a special empty value. Null is the default in the Java, and it is unchecked. Nothing in the type system warns you that a lookup might fail. The result is that a null can travel a long way from where it was created to where it finally blows up, and the stack trace points at the wrong line.
Returning Optional<T> says in the signature that a result might be missing. The caller has to deal with it, and the compiler makes sure that it does.
import java.util.Optional;
public class OptionalBasics {
static Optional<String> findUser(String id) {
if (id.equals("ana")) {
return Optional.of("Ana Lovelace");
}
return Optional.empty();
}
public static void main(String[] args) {
Optional<String> found = findUser("ana");
Optional<String> missing = findUser("zoe");
System.out.println("found present : " + found.isPresent());
System.out.println("missing present : " + missing.isPresent());
System.out.println("value or default : " + found.orElse("nobody"));
System.out.println("value or default : " + missing.orElse("nobody"));
found.ifPresent(name -> System.out.println("hello " + name));
missing.ifPresent(name -> System.out.println("never printed"));
System.out.println("empty is equal to a null value : " + missing.equals(Optional.empty()));
}
}The output is this.
found present : true
missing present : false
value or default : Ana Lovelace
value or default : nobody
hello Ana Lovelace
empty is equal to a null value : trueThere are two details in that output that are worth understanding. The call Optional.empty() is not the same as Optional.of(null), because the latter throws immediately. An Optional that holds null is a contradiction. And isPresent is the check that you will use the most often, though the more expressive methods are usually the better ones.
How do you create one?
| Method | Use when | Fails when |
|---|---|---|
Optional.of(value) | You know the value is present | The value is null |
Optional.ofNullable(value) | The value may be null | Never |
Optional.empty() | There is genuinely no value | Never |
The method ofNullable is the one that you will use constantly when you wrap a lookup that might miss. The distinction between of and ofNullable is not pedantry. It forces you to think about whether null is a real possibility at that point, and it fails loudly if you were wrong.
How do you read the value without a null check?
There are several ways, and they are not interchangeable.
orElse(default)always evaluates the default, so use it only when the default is cheap.orElseGet(supplier)evaluates the default only when the Optional is empty - the right choice for anything expensive.ifPresent(consumer)runs the action only when a value is present.get()returns the value or throws - acceptable only when you have already checkedisPresent.
import java.util.Optional;
public class OptionalMapping {
static Optional<String> rawName(String id) {
return id.startsWith("a") ? Optional.of(id.toUpperCase()) : Optional.empty();
}
public static void main(String[] args) {
Optional<Integer> length = Optional.of("tutorsbot").map(String::length);
System.out.println("mapped : " + length.orElse(-1));
Optional<Integer> missing = Optional.<String>empty().map(String::length);
System.out.println("mapped from empty : " + missing.orElse(-1));
Optional<Integer> upperLength = rawName("ana").map(String::length);
System.out.println("chained : " + upperLength.orElse(-1));
Optional<Optional<String>> nested = Optional.of(Optional.of("inner"));
System.out.println("flatMap : " + nested.flatMap(v -> v).orElse("none"));
java.util.function.Supplier<String> fallback = () -> "generated";
String computed = Optional.<String>empty().orElseGet(fallback);
System.out.println("orElseGet : " + computed);
}
}The output is this.
mapped : 9
mapped from empty : -1
chained : 3
flatMap : inner
orElseGet : generatedNotice the difference between the two default methods. The call orElse takes a value that is already computed, so the work happens whether you need it or not. The call orElseGet takes a supplier, so the work only happens on the empty path. With a cheap default like a constant string the difference does not matter. With a database call or a formatted string it matters a great deal.
How do you transform the value inside?
The method map applies a function to the value if there is one, and it returns an empty Optional otherwise. It is how you chain the calls without losing the "might be absent" information. The method flatMap is the version for a function that itself returns an Optional, and it flattens the result so that you do not end up with an Optional of an Optional.
import java.util.List;
import java.util.Optional;
public class OptionalStreams {
record Order(String id, double amount) { }
static Optional<Order> largest(List<Order> orders) {
return orders.stream().max((a, b) -> Double.compare(a.amount(), b.amount()));
}
public static void main(String[] args) {
List<Order> orders = List.of(new Order("A-1", 12.5), new Order("A-2", 40.0));
System.out.println("largest : " + largest(orders).map(Order::id).orElse("none"));
System.out.println("largest of empty : " + largest(List.of()).map(Order::id).orElse("none"));
System.out.println("first id : " + orders.stream().findFirst().map(Order::id).orElse("none"));
}
}The output is this.
largest : A-2
largest of empty : none
first id : A-1The last two lines of that program show the natural use of Optional with the streams. The methods max and findFirst already return an Optional, so the map converts it to the piece of data that you actually want, and the orElse supplies the fallback at the very end. That pattern keeps the "no result" case explicit all the way down the chain.
What are the rules that keep Optional from going wrong?
Optional is often misused because it is easy to apply everywhere. These rules come from the years of the code review experience.
- Do not use
get()without checkingisPresent()first. If you have to check, then you usually wantorElseinstead. - Do not use Optional as a field type or a method parameter. It is designed for the return values.
- Do not wrap a collection that may simply be empty. Return the empty collection instead.
- Do not nest the Optionals. Use
flatMapwhen the function returns one. - Do not use Optional for the primitive types where a sentinel or a wrapper is clearer.
import java.util.Optional;
public class OptionalPitfalls {
static Optional<String> findName(String id) {
return Optional.ofNullable(id);
}
public static void main(String[] args) {
Optional<String> present = findName("ana");
Optional<String> absent = findName(null);
System.out.println("present isPresent : " + present.isPresent());
System.out.println("absent isPresent : " + absent.isPresent());
System.out.println("get on present : " + present.get());
try {
present.get();
System.out.println("get does not throw");
} catch (java.util.NoSuchElementException e) {
System.out.println("get on absent throws : " + e.getClass().getSimpleName());
}
System.out.println("filter keeps : " + present.filter(name -> name.length() > 5).isPresent());
System.out.println("filter drops : " + present.filter(name -> name.length() > 50).isPresent());
}
}The output is this.
present isPresent : true
absent isPresent : false
get on present : ana
get does not throw
filter keeps : false
filter drops : falseThe call get() that throws is the failure mode that the whole class exists to prevent. If you find yourself writing isPresent() followed by get(), then you have gained nothing over a null check and you have paid for the extra object.
The habits to keep
- Return Optional from a method that may not find a result.
- Use of when the value cannot be null and
ofNullablewhen it can. - Prefer
orElseGetoverorElsefor anything costly. - Use map to chain the work without losing the empty case.
- Never call
get()on a value that you have not checked.
Try it yourself: write a method that looks up a customer by the id in a map and that returns Optional<Customer>. Write three callers. One of them prints a greeting with ifPresent, one of them substitutes a default name with orElse, and one of them builds a summary line with map. Then add a customer whose record is incomplete and show how filter lets you reject it before it reaches the rest of the code.