Chapter 6. Optional and Monads

In functional languages, there is a data type called the option type, also called the maybe type. Its purpose is to encapsulate a value, which is returned from a function. This option return type may indicate that there is no return value. The problem with returning a value such as zero or null is that these may be legitimate return values. With an option type, we can return a good value or indicate that nothing is returned.

In Java 8, the Optional class has been added to serve as an option type. It is used to wrap an object and possesses a number of methods that can reflect a non-value and deal with these values. The calls allow us to handle null values and avoid the dreaded null pointer exception. The Optional

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

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.