Optional or Else Get() method in Java with examples
The ElseGet() is the method in the java.util.Optional class. To find the value of an Optional instance in Java, if any, use the optional class. This function returns the value created from the given supplier if there is no value in this Optional instance.
Syntax:
public T orElseGet(Suppliersupplier)
Parameters: In the case that this Optional instance has no value, this method takes the supplier as a parameter of type T and generates a value to return.
Return: In a situation where this Optional instance is available, this function returns its value. This function returns the value created from the designated supplier if there is no value in this Optional instance.
Exception: This function throws NullPointerException in the case that this Optional instance contains no values.
The below are the two different examples of the orElseGet()method.
Example 1: If Value is Present
This Java code demonstrates the use of Optional's orElseGet function, which returns a substitute value if the Optional is empty. Here, an Optional is generated with the given value. If the Optional is empty, the orElseGet function is used with a lambda expression to generate a random integer between 0 and 9. Lastly, it prints the result of applying orElseGet as well as the initial Optional value.
Implementation:
FileName: OptionalElseGetValue.java
import java.io.*; import java.util.*; import java.util.function.*; public class OptionalElseGetValue { public static void main(String[] args) { //Creating an Optional Optionalo = Optional.of(10455); // displaying the supplier System.out.println("The Optional value is: " + o); // Implementing the orElseGet supplier System.out.println("The Value after orElseGet" + "(Supplier) method: " + o.orElseGet(() -> (int)(Math.random() * 10))); } }
Output:
The Optional value is: Optional[10455] The Value after orElseGet(Supplier) method: 10455
Example 2: If No Value is Present
This Java code demonstrates how to use the orElseGet method of an Optional when the Optional is empty. An empty Optional is initially generated. It tries to get a value by calling orElseGet, which should run the lambda expression that's been passed and returns a random integer between 0 and 9. But in this instance, the lambda expression isn't executed because the Optional is empty; therefore the default value of 0 is used instead. Lastly, it prints the result of applying orElseGet as well as the initial Optional value.
Implementation:
FileName: OptionalElseGetNoValue.java
import java.io.*; import java.util.*; import java.util.function.*; public class OptionalElseGetNoValue { public static void main(String[] args) { //Creating a Optional Optionalo = Optional.empty(); // displaying the supplier System.out.println("The Optional Value: " + o); try { // Implementing the orElseGet supplier System.out.println("The Value after orElseGet" + "(Supplier) method: " + o.orElseGet(() -> (int)(Math.random() * 10))); } catch (Exception ex) { System.out.println(ex); } } }
Output:
The Optional Value: Optional.empty The Value after orElseGet(Supplier) method: 0