Summary

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 ...

Get Learning Java Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.