You cannot call orElse
after ifPresent
, the reason is, orElse
is called on an optiional but ifPresent
returns void. So the best approach to achieve is ifPresentOrElse
.
It could be like this:
op.ifPresentOrElse(
(value)
-> { System.out.println(
"Value is present, its: "
+ value); },
()
-> { System.out.println(
"Value is empty"); });