Skip to content
shaynagy edited this page Mar 31, 2022 · 4 revisions

Optional

https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

To deal with missing or null values by abstracting null checks

Creating Optional objects

  • of(), empty() → use ofNullable()

Useful Methods

  • ifPresent()
  • orElse(), orElseGet()
  • map(), flatMap()

Notes: Use if-else approach for declarative programming; Avoid get(), isPresent(), isEmpty() (breaks Tell-Don’t-Ask principle)

Stream