Java Reference ยท Streams and Functional
Java Optional.orElse() and orElseThrow()
Reference for getting a value out of a Java Optional: get(), orElse(), orElseGet(), orElseThrow() and or(), with the eager-versus-lazy difference explained.
Once you have an Optional, you eventually need the value inside, or a sensible fallback. Java gives you several ways to unwrap it, and they differ in what happens when the Optional is empty.
orElse(other) returns a default, orElseGet(supplier) computes the default only when needed, and orElseThrow() throws an exception. get() also exists, but it is the one to avoid: its name hides the fact that it can throw. Since Java 10, orElseThrow() with no argument is the clear, preferred replacement for get().
String name = optionalName.orElse("anonymous"); String id = optionalId.orElseThrow(() -> new IllegalStateException("no id"));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public T get() public T orElse(T other) public T orElseGet(Supplier<? extends T> supplier) public T orElseThrow() public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
| Method | If present | If empty |
|---|---|---|
get() |
the value | throws NoSuchElementException |
orElse(other) |
the value | other (always evaluated before the call) |
orElseGet(supplier) |
the value | the supplier's result (supplier called only now) |
orElseThrow() |
the value | throws NoSuchElementException (Java 10) |
orElseThrow(supplier) |
the value | throws the exception the supplier creates |
or(supplier) |
this Optional | the Optional the supplier returns (Java 9) |
Throws: NoSuchElementException for get() and orElseThrow(); any X for the supplier variant. When the Optional is empty, orElseThrow(supplier) throws NullPointerException if the supplier itself returns null.
Usage notes
orElseis eager. Its argument is evaluated before the method runs, whether or not the Optional is empty. If building the default has a cost or a side effect (database call, logging, object creation), useorElseGet.- Cheap constants are fine with
orElse.orElse("")ororElse(0)creates nothing expensive, and it reads better than a lambda. - Prefer
orElseThrow()overget(). They behave identically, but the name tells readers an exception is possible. Tools and linters often flagget()without a priorisPresent(). - Unchecked or checked. The supplier version lets you throw a checked exception too; the method's
throws Xclause then forces callers to handle it. isPresent()thenget()is usually a sign you wantorElse*,maporifPresentinstead.- Returning
nullfromorElse(null)is allowed, but turns the Optional back into a nullable reference; keep it at boundaries with old APIs. or()returns an Optional, so it chains fallbacks without unwrapping: first cache, then database, then default.