© Ralph Lecessi 2019
Ralph LecessiFunctional Interfaces in Javahttps://doi.org/10.1007/978-1-4842-4278-0_11

11. Use in Optionals

Ralph Lecessi1 
(1)
Kendall Park, NJ, USA
 

It is useful to wrap an object inside an Optional to avoid checking for nullness. Optionals can also be used inside method chains to simplify the logic of a program.

Section 11.1: Creating an Optional

The Optional class wraps an object of type parameter T.
public final class Optional <T> {
   ...
}
The Optional class provides the following static methods which are used to create Optionals of type T:
static <T> Optional<T> of(T value);
static <T> Optional<T> ofNullable(T value);
static <T> Optional<T> empty();
The of method creates an Optional from a non-null argument. If called with a null ...

Get Functional Interfaces in Java: Fundamentals and Examples 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.