Optional.of(null)will throw NPE, I need to verify null before call the method?
Use Optional.ofNullable
if you are unsure whether you have a value or not, but you do not want a NullPointerException
to be thrown.
Use Optional.of
if you know you have a non-null-value or if it's ok for you if a NullPointerException
is thrown otherwise.
Regarding the rest of your question: why null
or Optional
you may find the following question useful: Optional vs. null. What is the purpose of Optional in Java 8?
Your question may also be related to: Why use Optional.of over Optional.ofNullable?