Optional

A container that holds either one value or nothing, used as a return type to make "no result" visible to the caller.

In more detail

Optional.of wraps a value that must not be null, Optional.empty represents absence, and Optional.ofNullable adapts an older API that may return null. map, filter and flatMap transform the value only if it is present, and orElse supplies a fallback. Use it for lookups that may find nothing; do not use it for every field or parameter, and do not call get without knowing a value exists.

Examples

Optional<String> value = Optional.empty();
find().map(String::toUpperCase).orElse("MISSING")

Related terms

Taught in

References

Certification courses