Problem Statement
List two good practices and two anti-patterns when using Optional in Java.
Explanation
Good: return Optional from methods that may be empty; use terminal operations like orElse, orElseGet, orElseThrow instead of get(). Good: chain map/flatMap/filter to avoid nested ifs. Anti-patterns: use Optional for fields or parameters (adds overhead and ambiguity); call get() without checking presence; use Optional in performance-critical collections.
Practice Sets
This question appears in the following practice sets: