Optional if Present Or Else() method in Java with Examples
Java.util.Optional class provides an ifPresentOrElse(Consumer, Runnable) function. The Optional class uses the value of this Optional object to support us in performing the targeted Consumer action. The second parameter, an empty-based Runnable called emptyAction, is sent to this method in the event that no value is present in this Optional.
- Both the Runnable and Consumer interfaces are implemented using this framework.
- The value will be present when the Consumer interface's accept method is called. The accept method takes a parameter, which is the value of the optional instance.
- If the value is missing, the Runnable interface's run function will be invoked with no parameters.
Syntax:
public void ifPresentOrElse(Consumeraction, Runnable emptyAction)
-where the parameters can be defined as
action: which, in the case where a value is present, is the action to be taken on this Optional.
emptyAction: which, should there be no value, is the empty-based action that needs to be performed out.
Return value: No value is returned by this method
Exception: If a value is present and the provided action is null, or if no value is present and the provided empty-based action is null, then this method throws a NullPointerException.
Note: As Java 9 introduces this method, programs that use it must run on JDK 9.
The below are the two different examples or cases for ifPresentOrElse() method.
Example 1: If Value is Present
This Java method shows how to use the Optional class in Java. It generates an Optional with the given integer value in it. The presence or absence of the value within the Optional is then handled using the ifPresentOrElse function. It prints the appropriate value after the value if the value is there; if not, it returns that there is no value. Although NullPointerExceptions are prevented explicitly when a value is missing, this method helps prevent them.
Implementation:
FileName: OptionalSingleParameter.java
import java.io.*; import java.util.*; public class OptionalSingleParameter { public static void main(String[] args) { //Creating an Optional Optionalo = Optional.of(10455); // displaying the value System.out.println("The Optional value is: " + o); // implement the ifPresentOrElse o.ifPresentOrElse( (value) -> { System.out.println( "Yes, Value is present it is: " + value); }, () -> { System.out.println( "Value is empty"); } ); } }
Output:
The Optional value is: Optional[10455] Yes, Value is present it is: 10455
Example 2: If Value is not Present
This Java code demonstrates how to use an empty value for the Optional class. It begins with a blank Optional
Implementation:
FileName: NoValue.java
import java.io.*; import java.util.*; public class NoValue { public static void main(String[] args) { //Creating the Optional Optionalop = Optional.empty(); // displaying the value System.out.println("The Optional value is: " + op); try { // implementing the ifPresentOrElse op.ifPresentOrElse( (value) -> { System.out.println( "Yes, Value is present, it is: " + value); }, () -> { System.out.println( "Value is empty"); } ); } catch (Exception e) { System.out.println(e); } } }
Output:
The Optional value is: Optional.empty Value is empty