Java Optional and orElse

breakline :

I'm new to the Java optionals but I see this code written by another developer and I don't get it:

String t = null;
Optional.ofNullable("notnull")
    .orElse(
        Optional.ofNullable(t).orElseThrow(() -> new Exception("MyException"))
    );

Why does this code throw the exception? Why does it even go to the "orElse" branch?

Is this because of some weird execution order? So the first optional's value is not set before the orElse branch is evaluated?

whitebrow :

The orElse stream is never invoked but the method itself is executed. This means that the method parameter is passed to it as well. So the part Optional.ofNullable(t).orElseThrow(() -> new Exception("MyException")) is being called regardless of the value passed to the first Optional.ofNullable call.

If you don't want this to happen you will need to pass a Supplier like this:

String t = null;
Optional.ofNullable("notnull")
    .orElseGet(
        () -> Optional.ofNullable(t).orElseThrow(() -> new RuntimeException("MyException"))
    );

The supplier is only invoked when the orElseGet stream is invoked. Note that you'll need a RuntimeException instead of a checked exception in order to be able to break from the supplier.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=138746&siteId=1