{{announcement.body}}
{{announcement.title}}

Optional.orElse() Vs. Optional.orElseGet()

DZone 's Guide to

Optional.orElse() Vs. Optional.orElseGet()

Learn more about programming Java Optionals.

· Java Zone ·
Free Resource

Programming Java optionals

Learn more about programming Java Optionals

A very common misunderstanding, as well as bad usage, can prevail if you simply go by the dictionary and semantic definition of Optional.orElse() and Optional.orElseGet().

A smart man makes a mistake, learns from it, and never makes that mistake again. But a wise man finds a smart man and learns from him and how to avoid the mistake altogether.

The difference is pretty subtle, and if you don't pay close attention, then you will continue to make the same mistakes.

You may also like: 26 Reasons Why Using Optional Correctly Is Not Optional

The best way to understand the difference between orElse() and orElseGet() is that orElse() will always be executed if the Optional<T> is null or not. But orElseGet() will only be executed when Optional<T> is null.

The dictionary meaning of orElse is: "execute the part when something is not present," but here it contradicts that, as shown in the following example:

    Optional<String> nonEmptyOptional = Optional.of("Vishwa Ratna");
    String value = nonEmptyOptional.orElse(iAmStillExecuted());

    public static String iAmStillExecuted(){
    System.out.println("nonEmptyOptional is not NULL,still I am being executed");
    return "I got executed";
    }


Output: nonEmptyOptional is not NULL, but still, I am executing:

    Optional<String> emptyOptional = Optional.ofNullable(null);
    String value = emptyOptional.orElse(iAmStillExecuted());
    public static String iAmStillExecuted(){
    System.out.println("emptyOptional is NULL, I am being executed, it is normal as 
    per dictionary");
    return "I got executed";
    }


OutputemptyOptional is NULL. I am now executing it as usual, as per its dictionary definition.

For orElseGet(), the method follows its dictionary meaning. The orElseGet() part will be executed only when the Optional is null.

Benchmarks:

+--------------------+------+-----+------------+-------------+-------+
| Benchmark          | Mode | Cnt | Score      | Error       | Units |
+--------------------+------+-----+------------+-------------+-------+
| orElseBenchmark    | avgt | 20  | 60934.425  | ± 15115.599 | ns/op |
+--------------------+------+-----+------------+-------------+-------+
| orElseGetBenchmark | avgt | 20  | 3.798      | ± 0.030     | ns/op |
+--------------------+------+-----+------------+-------------+-------+


Remarks: orElseGet() has clearly outperformed orElse() for our particular example.

Hopefully, this post clears up any doubts from people like me who need a very basic example. Please share your thoughts in the comments.

Further Reading

Java 8 Optional Usage and Best Practices

26 Reasons Why Using Optional Correctly Is Not Optional

A Look at Java Optionals

Topics:
java 1.8 ,java ,optional ,java optionals ,fp ,java 8 ,tutorial ,orelse

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}