ifPresentOrElse() method of Optional object in Java

This method was introduced from Java 9.

This method is similar to the ifPresent() method which I introduced in this tutorial, but here is a difference since we have one more Runnable parameter, so that in-case if the Optional object is empty, the object of the Runnable interface will be executed.

For example:

Result:

ifPresentOrElse() method of Optional object in Java

 

Add Comment