Using Optional
— Nowhere, Somewhere, or Everywhere?
Java 8 introduced Optional
for a very precise use case: to allow the continuation of Stream
pipelines without triggering a NullPointerException
. This is NPE-safe:
Stream.of(2, 4, 6)
.filter(this::isOdd) //
…