How to make ‘map:: get’ return ‘optional’ or ‘optional’ of the found value empty()`

I'm trying to do this:

return Optional.of(myMap.getOrDefault(myKey,null));

Really, what I want is to return optional if found Of (foundval), otherwise it returns optional empty(). I don't believe in optional Of (null) is equivalent to this What grammar do I want to do?

That is, how to get the map to return to the correct options?

Solution

Why not simply:

return Optional.ofNullable(myMap.get(myKey));

JavaDocs

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>