In this post we are going to cover working with the Optional class introduced in Java 8. The introduction of
Optional was new only to Java. Guava has had a version of Optional and Scala has had the Option type for some time. Here’s a description of Optional from the Java API docs:
A container object which may or may not contain a non-null value.
Since so many others have done a good job of describing the
Optional type, we won’t be doing so here. Rather for this post, we are going to cover how to use the
Optional type without resorting to directly accessing the value contained or doing explicit checks if a value is present.