The Optional
class provides a better way of dealing with null and missing values. In many situations, it can be used to avoid the dreaded null pointer exceptions. This class supports the fluent style of programming.
We illustrated how to create and use Optional
instances. The ability to return default values was illustrated using the or else type of methods. The orElseThrow
method, in particular, is useful for dealing gracefully with exceptions. We are able to transform and filter Optional
objects as illustrated using the map
and filter
methods.
Monads were introduced, and are used to chain a series of functions together using a fluent style. A monad encapsulates a value, allows that value to be transformed using operations, and permits these operations to be cascaded together. You learned that monads follow certain laws.
In the next chapter, we will investigate how the functional elements of Java 8 can be used to implement various design patterns. This presents new and exciting ways...