- Optional.empty() - An empty optional
- Optional.of(t) - returns a present Optional containing t. (t must be non-null)
- Optional.ofNullable(t) - returns an Optional with t that can be null
- Never return null from a method that's supposed to return an optional. It defeats the purpose of Optional
- Never do
opt.get()
unless you can prove that the optional is present. - Prefer alternatives to using
opt.isPresent()
followed byopt.get()
- It's generally a bad idea to create an Optional for the sole purpose of chaining methods from it to get a value.
- If an Optional chain is nested or has an intermediate result of Optional<Optional>, it is probably too complex.