Difference between `Optional.orElse()` and `Optional.orElseGet()`

I am trying to understand the difference between the Optional<T>.orElse() and Optional<T>.orElseGet() methods.

The description for the orElse() method is “Return the value if present, otherwise return other.”

While, the description for the orElseGet() method is “Return the value if present, otherwise invoke other and return the result of that invocation.”

The orElseGet() method takes a Supplier functional interface, which essentially does not take any parameters and returns T.

In which situation would you need to use orElseGet()? If you have a method T myDefault() why wouldn’t you just do optional.orElse(myDefault()) rather than optional.orElseGet(() -> myDefault()) ?

It does not seem that orElseGet() is postponing the execution of the lambda expression to some later time or something, so what’s the point of it? (I would have thought that it would be more useful if it returned a safer Optional<T> whose get() never throws a NoSuchElementException and isPresent() always returns true… but evidently its not, it just returns T like orElse()).

Is there some other difference I am missing?

9 Answers
9

Leave a Comment