Home > Java > javaTutorial > When Should I Use Optional.orElseGet() Instead of Optional.orElse()?

When Should I Use Optional.orElseGet() Instead of Optional.orElse()?

Mary-Kate Olsen
Release: 2024-11-02 09:38:31
Original
853 people have browsed it

 When Should I Use Optional.orElseGet() Instead of Optional.orElse()?

Understanding the Difference between Optional.orElse() and Optional.orElseGet()

Optional.orElse() and Optional.orElseGet() are two methods in Java that help in handling Optional, a container object that may or may not contain a value.

Description of orElse()

orElse() takes a value (other) as an argument. If the Optional is present (contains a value), it returns that value. However, if the Optional is empty, it returns the provided value (other).

Description of orElseGet()

orElseGet() also takes an argument, but this time it's a Supplier functional interface. A Supplier takes no arguments and returns a value. Unlike orElse(), orElseGet() will only invoke the Supplier function when the Optional is empty.

When to Use orElseGet()?

The main difference between orElse() and orElseGet() is when the provided argument would result in an expensive operation. If the Optional is not empty, using orElse() will still perform the provided operation even though it's unnecessary. However, orElseGet() avoids this unnecessary operation, only executing it when needed.

Example

Consider the following example:

<code class="java">Optional<String> myString = Optional.empty();

String result1 = myString.orElse(ExpensiveOperation.getExpensiveString()); // ExpensiveOperation always executed
String result2 = myString.orElseGet(() -> ExpensiveOperation.getExpensiveString()); // ExpensiveOperation only executed if Optional is empty</code>
Copy after login

Test Case

To illustrate the difference, consider a function that returns an Optional string:

<code class="java">public Optional<String> findMyPhone(int phoneId) { ... }</code>
Copy after login

Scenario 1: orElse()

Optional.isPresent() Used phone
True (redundant call) NewExpensivePhone
False NewExpensivePhone

Scenario 2: orElseGet()

Optional.isPresent() Used phone
True MyCheapPhone
False NewExpensivePhone

As seen in the test case, orElse() always executes the provided function, while orElseGet() only executes it when needed.

Conclusion

Understanding the difference between orElse() and orElseGet() is crucial when working with Optionals. orElseGet() provides an efficient way to handle Optional values by avoiding unnecessary operations when the Optional is present.

The above is the detailed content of When Should I Use Optional.orElseGet() Instead of Optional.orElse()?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template