OptionalDouble ifPresent(DoubleConsumer) method in Java with examples

The ifPresent(java.util.function.DoubleConsumer) method helps us to perform the specified DoubleConsumer action the value of this OptionalDouble object. If a value is not present in this OptionalDouble, then this method does nothing.Â
Syntax:
public void ifPresent(DoubleConsumer action)
Parameters: This method accepts a parameter action which is the action to be performed on this Optional, if a value is present.Â
Return value: This method returns nothing.Â
Exception: This method throw NullPointerException if a value is present and the given action is null.Â
Below programs illustrate ifPresent(DoubleConsumer) method:Â
Program 1:Â
Java
// Java program to demonstrate// OptionalDouble.ifPresent(DoubleConsumer) methodÂ
import java.util.OptionalDouble;Â
public class GFG {Â
    public static void main(String[] args)    {Â
        // create a OptionalDouble        OptionalDouble opdouble = OptionalDouble.of(0.23425);Â
        // apply ifPresent(DoubleConsumer)        opdouble.ifPresent((value) -> {            value = Math.pow(value, 4);            System.out.println("Value after modification:=> "                               + value);        });    }} |
Output: 
Java
// Java program to demonstrate// OptionalDouble.ifPresent(DoubleConsumer) methodÂ
import java.util.OptionalDouble;Â
public class GFG {Â
    public static void main(String[] args)    {Â
        // create a OptionalDouble        OptionalDouble opdouble = OptionalDouble.empty();Â
        // apply ifPresent(DoubleConsumer)        opdouble.ifPresent((value) -> {            System.out.println("Value:=> "                               + value);        });Â
        System.out.println("As OptionalDouble is empty value"                           + " is not modified");    }} |
Output: 



