Java 8 - general practice using Optional
0. Never, ever use null for an Optional variable.\
-
- 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()