Hi I am using Java Optional. I saw that the Optional has a method ifPresent.
Instead of doing something like:
Optional object = someMeth
You need to do two things:
Optional
into an Optional
, which has a value iff the original Optional had a value. You can do this using map: object.map(MyObject::toString)
(or whatever other method/function you want to use).Optional
, or else return a default if the Optional doesn't have a value. For that, you can use orElseCombining these:
String myValue = object.map(MyObject::toString).orElse(null);