Java Optional working of orElse is not as if else

Viewed 2410

In Optional while optional.orElse method is call, irrespective of the element is present or not the orElse part is executed it does not behave as the if else condition.

In the below code if you see in Case 1 both getNullPoJo and getDefaultPoJo is executed since getNullPoJo will return empty Optional

In Case 2 where you will get a Optional with loaded value (from getLoadedPoJo) also you getDefaultPoJo is executed

I am just trying to understand the working of optional.orElse.

public static void main (String [] a) {
    PoJo poJo1=getNullPoJo().orElse(getDefaultPoJo());//Case 1
    System.out.println("pojo1 Got "+poJo1.getVariable());
    PoJo poJo2=getLoadedPoJo().orElse(getDefaultPoJo());//Case 2
    System.out.println("pojo2 Got "+poJo2.getVariable());
}

private static Optional<PoJo> getNullPoJo() {
    System.out.println("Executing getNullPoJo");
    Optional<PoJo> optional=Optional.empty();
    return optional;
}

private static Optional<PoJo> getLoadedPoJo() {
    System.out.println("Executing getLoadedPoJo");
    PoJo poJo =new PoJo();
    poJo.setVariable("Loaded");
    Optional<PoJo> optional=Optional.of(poJo);
    return optional;
}

private static PoJo getDefaultPoJo() {
    System.out.println("Executing getDefaultPoJo");
    PoJo poJo =new PoJo();
    poJo.setVariable("Default");
    return poJo;
}

The current Output is:

Executing getNullPoJo

Executing getDefaultPoJo

pojo1 Got Default

Executing getLoadedPoJo

Executing getDefaultPoJo

pojo2 Got Loaded

My Expected Output is:

Executing getNullPoJo

Executing getDefaultPoJo

pojo1 Got Default

Executing getLoadedPoJo

pojo2 Got Loaded

I do not want the call to getDefaultPoJo in Case 2

3 Answers

Use orElseGet() to avoid evaluating getDefaultPoJo() when the Optional is not empty:

PoJo poJo1=getNullPoJo().orElseGet(() -> getDefaultPoJo());
PoJo poJo2=getLoadedPoJo().orElseGet(() -> getDefaultPoJo());
getNullPoJo().orElse(getDefaultPoJo());

It's a method chain, and every method in this chain will get executed, no matter how the underlying API is supposed to work.

1) getNullPoJo()
2) r = getDefaultPoJo()
3) orElse(r)  

In order to execute a method, its actual parameters must be evaluated. To call orElse(getDefaultPoJo()), getDefaultPoJo() must be invoked as well. That's the reason you are getting more than you expected.

Usually, you will see

.orElse(null);
.orElse(defaultValue);

where null, and defaultValue are predefined values that don't require any calculations.

On the other hand, we write

.orElseGet(() -> generateDefaultValue());
.orElseGet(() -> calculateDefaultOutcome());

where generateDefaultValue and calculateDefaultOutcome are methods that do perform some calculations (intensive ones or ones we don't want to execute until the right moment [your case]).

Compare,

.orElseGet(() -> createDefaultPoJo());
.orElse(DEFAULT_POJO);

where DEFAULT_POJO is a variable initialised prior to this method call, and createDefaultPoJo() is a method that creates a default instance every time it gets called.

Related