ifPresent() method of Optional object in Java

This method is used to check whether an Optional object is empty or not? If not, then do the operations with this object, otherwise do not do anything.

The parameter of this method is an object of the Consumer interface. You can see more about the Consumer interface here.

For example:

Result:

ifPresent() method of Optional object in Java

Add Comment