Conditionals using Optional

Is there a way to change the conditional statement using Optional library?

Optional<String> fruit = Optional.of("Apple");
fruit.isPresent() ? "Fruit Present" : "Fruit not present";

>Solution :

It is little bit weird since argument of map function is ignored, anyway it is equivalent to ?:.

fruit.map(currentElement -> "Fruit Present").orElse("Fruit not present")

Leave a Reply