Optional were added to Java 8 to represent a container object which may or may not contain a non null value.
Java Optional Tutorial Series
I have written a complete guide to use Optional. It features what Optional is, How to use it, How not to use it and finally How to test methods that return an Optional?
Optional Introduction
How to use Optional
- How to create Optional Object?
- orElse() family : Getting value out of Optional
- orElse vs orElseGet
- ifPresent() family : Taking action on value if present in Optional.
- filter, map and flatMap : Filtering and Transforming Optional value.
- Collecting data from Optional using Collectors
- equals, hashCode and toString
- Combining isPresent() and get() is an anti-pattern
How not to use Optional
- Don’t put code block in Optional
- Java 8 Optional Code Smell with Stream
- Don’t wrap Collection in Optional