The reason for this is the use of Optional<T>
in the return. Optional is not allowed to contain null
. Essentially, it offers no way of distinguishing situations “it’s not there” and “it’s there, but it is set to null
“.
That’s why the documentation explicitly prohibits the situation when null
is selected in findFirst()
:
Throws:
NullPointerException
– if the element selected isnull