Optional – orElse(), orElseGet() and orElseThrow() methods

In this lesson, we will cover the three useful methods from the Optional class:

  1. public T orElse(T other) – It returns the value if present, otherwise returns the other.
  2. public T orElseGet(Supplier<? extends T> other) – It returns the value if present. Otherwise, invoke other and return the result of that invocation.
  3. public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) – It returns the contained value, if present, otherwise, throw an exception to be created by the provided supplier.

1. Using the orElse() method

class Test {

  public static void main(String[] args) {
    Optional<String> optional = Optional.ofNullable("value1");

    String valueFromOptional = optional
            .map(value -> value.toUpperCase()) // if the value is not null, return the transformed value
            .orElse("default value"); // if the value is null, return the "default value"

    System.out.println(valueFromOptional);
  }
}
Output: VALUE1
 
If we pass the null value to the Optional.ofNullable() method, we’ll get an empty Optional.

class Test {

  public static void main(String[] args) {
    String strValue = null;
    Optional<String> optional = Optional.ofNullable(strValue);

    String valueFromOptional = optional
            .map(value -> value.toUpperCase()) // if the value is not null, return the transformed value
            .orElse("default value"); // if the value is null, return the "default value"

    System.out.println(valueFromOptional);
  }
}
Output: default value
 
We got “default value” in the output because we tried to perform the map() operation on empty Optional.

2. Using the orElseGet() method

This method is slightly different from the previous one because it accepts a Supplier.

Example:

class Test {

  public static void main(String[] args) {
    Optional<String> optional = Optional.ofNullable(null);

    String valueFromOptional = optional
            .map(value -> value.toUpperCase()) // if the value is not null, return the transformed value
            .orElseGet(() -> "this is a default value"); // if the value is null, execute the Supplier

    System.out.println(valueFromOptional);
  }
}
Output: this is a default value

3. Using the orElseThrow() method

Example:

class Test {

  public static void main(String[] args) {
    Optional<String> optional = Optional.ofNullable(null);

    String valueFromOptional = optional
            .map(value -> value.toUpperCase()) // if the value is not null, return the transformed value
            .orElseThrow(() -> new RuntimeException("Optional is empty!")); // if the value is null, throw the exception

    System.out.println(valueFromOptional);
  }
}
Output: Exception in thread “main” java.lang.RuntimeException: Optional is empty! at com.company.Test.lambda$main$1(User.java:14) at java.base/java.util.Optional.orElseThrow(Optional.java:408) at com.company.Test.main(User.java:14)
 
As you can see, we got the specified exception when we tried to perform the map() operation on an empty Optional.
 
I hope this tutorial was helpful to you. To learn more, check out other tutorials that teach Java Optional.
 
Happy learning!

Leave a Reply

Your email address will not be published. Required fields are marked *