Optional type (Java)
How and when to use the Java Optional type
Basic idea
Optional<T>
: a wrapper for either an object of type T
or no object
Goal: provide safer alternative to returning either an object of type T
or null
Main use case: return type for methods that do not necessarily produce a value
Note: Method parameters of type Optional
are not recommended because this makes it awkward to call the method. For implementing methods with optional parameters, method overloading is generally considered a better option. See also Why should Java 8's Optional not be used in arguments
How to use Optional values
Basically, two sensible options:
- use a method that produces an alternative value if the Optional is empty
- use a method that only consumes the value if the Optional is not empty
Examples first option:
Examples second option:
How not to use Optional values
When used in the wrong way, using Optional
is not safer or easier than using null
Examples:
Creating Optional values
Turning an Optional into a Stream
Conceptually, you can compare an Optional to a stream with either zero or one elements. The .stream()
method follows that principle, yielding a stream with zero elements if the Optional is empty and a stream with one element if the optional has a value
Example use case:
Note: if you call a method that returns either a value or null, you can apply the samle principle using Stream.ofNullable
:
Resources
- Core Java SE 9 for the Impatient (book by Cay S. Horstmann)
- Why should Java 8's Optional not be used in arguments