In Java, the Optional
class provides a way to handle possible null
values in a more functional style. If you’re using an Optional
and want to provide a default value, you can do so using the orElse()
or orElseGet()
methods. Here’s an explanation and code examples for both:
1. Using Optional.orElse()
The orElse()
method provides a default value that will be returned if the Optional
is empty.
Example:
package org.kodejava.util;
import java.util.Optional;
public class DefaultExample {
public static void main(String[] args) {
Optional<String> optionalValue = Optional.empty();
// If optional is empty, "Default Value" will be returned
String result = optionalValue.orElse("Default Value");
System.out.println(result); // Output: Default Value
}
}
Here:
- If the
optionalValue
is empty, the provided"Default Value"
will be returned. - If it contains a value, that value will be used instead.
2. Using Optional.orElseGet()
The orElseGet()
method works like orElse()
, but it accepts a Supplier
functional interface. This is useful if the default value requires some computation.
Example:
package org.kodejava.util;
import java.util.Optional;
public class DefaultExample1 {
public static void main(String[] args) {
Optional<String> optionalValue = Optional.empty();
// Use a Supplier for the default value
String result = optionalValue.orElseGet(() -> "Computed Default Value");
System.out.println(result); // Output: Computed Default Value
}
}
Here:
- The
orElseGet()
method evaluates the lambda expression (or Supplier) only if theOptional
is empty, making it more efficient if computation of the default value is expensive.
3. Key Differences:
orElse()
: The default value is always evaluated, even if theOptional
contains a value.orElseGet()
: The default value is lazily evaluated (only computed if needed, i.e., when theOptional
is empty).
Example to Show the Difference:
package org.kodejava.util;
import java.util.Optional;
public class DefaultExample2 {
public static void main(String[] args) {
Optional<String> optionalValue = Optional.of("Present");
// orElse: Supplier function is evaluated regardless of whether the Optional is empty or not
String result1 = optionalValue.orElse(getDefaultValue());
System.out.println(result1); // Output: Present (but still calls getDefaultValue())
// orElseGet: Supplier function is only evaluated if Optional is empty
String result2 = optionalValue.orElseGet(() -> getDefaultValue());
System.out.println(result2); // Output: Present (doesn't call getDefaultValue())
}
public static String getDefaultValue() {
System.out.println("Computing default value...");
return "Default Value";
}
}
4. Using Optional.orElseThrow()
An alternative is to throw an exception if the Optional
is empty instead of providing a default value.
Example:
package org.kodejava.util;
import java.util.Optional;
public class DefaultExample3 {
public static void main(String[] args) {
Optional<String> optionalValue = Optional.empty();
String result = optionalValue.orElseThrow(() -> new IllegalStateException("Value is missing"));
// Will throw the exception: IllegalStateException: Value is missing
System.out.println(result);
}
}
Summary:
- Use
orElse()
when you want to provide a default value directly. - Use
orElseGet()
when the default value requires expensive computation and should be lazily evaluated. - Use
orElseThrow()
if you want to throw an exception when theOptional
is empty.
Latest posts by Wayan (see all)
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025
- How to Use Java 17 Text Blocks for Multiline Strings - April 22, 2025