Java 8 - general practice using Optional

less than 1 minute read

0. Never, ever use null for an Optional variable.
1. Optional should be only used as a return value.
: never use it for a field in Class and a method parameter.
never use it in collections

2. Never try to call .get() method on an Optional.
: if it is a situation to get a value, ifPresent, map and orElse() family can solve this problem .

3. Remember Optional is not Serializable.

Note)
Java 9 will introduce the following new method

1
2
3
Optional.stream()
Optional.ifPresentOrElse()
Optional.or()