DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • RBAC With API Gateway and Open Policy Agent (OPA)
  • How to Implement Istio in Multicloud and Multicluster
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR
  • Tech Hiring: Trends, Predictions, and Strategies for Success

Trending

  • RBAC With API Gateway and Open Policy Agent (OPA)
  • How to Implement Istio in Multicloud and Multicluster
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR
  • Tech Hiring: Trends, Predictions, and Strategies for Success
  1. DZone
  2. Coding
  3. Java
  4. Optional... What Else?

Optional... What Else?

Optional comes with number of toys to play with. Here, we focus on how to properly use orElse, orElseGet, and other ways to make Optionals work for you.

Brian Vermeer user avatar by
Brian Vermeer
·
Jan. 03, 18 · Tutorial
Like (13)
Save
Tweet
Share
18.21K Views

Join the DZone community and get the full member experience.

Join For Free

Optional is in some ways, the Java implementation of the Maybe monad. Don’t get scared by the ‘M’ word. It is simply an encapsulation to handle a specific case of a type — in this case, the possibility of a null value. Just consider it a wrapper to force the user to check if the value is present or not.

When using an Optional in a declarative way, we are able to execute a map function on it. The lambda supplied with the map will only be executed if the Optional is filled with a value.

public void execute() {
    Optional<String> maybeString = Optional.of("foo");
maybeString.map(this::runIfExist);
}

private String runIfExist(String str) {
    System.out.println("only run if optional is filled ");
    return str;
}


When running the execute() method, the console output will obviously be:  
 only run if optional is filled 

If we change the code of maybeString to Optional.empty() and execute again, as expected, nothing happens.

Alternative Flow

That's all nice, but when we want an alternative flow for when the Optional is empty, we can choose from else(), elseGet(), and  elseThrow(). With the last one, we can throw an exception, but what if we want to do something different?

Intuitively, the following code looks quite obvious.

public void execute() {
    Optional<String> maybeString = Optional.of("foo");
    String newString = maybeString
            .map(this::runIfExist)
            .orElse(runIfEmpty());
    System.out.println(newString);
}

private String runIfExist(String str) {
    System.out.println("only run if optional is filled ");
    return str;
}

private String runIfEmpty() {
    System.out.println("only run if empty");
    return "empty";
}


By reading the code, we probably expect that newString will be given the value  "foo" because maybeString is an Optional containing a value. When running the execute()   method, the following output will end up in your console:

only run if optional is filled
only run if empty
foo

We can still conclude that the newString will end up with the value "foo". The other thing we can see over here is that the string "only run if empty" is also printed to the console. That was something I didn't expect at first sight. So even if the Optional contains a value, the method inside the orElse() is still being executed. 

When we change the execute() method and use orElseGet() instead of orElse(), we also need to change the method call to runIfEmpty() to a supplier:

public void execute() {
    Optional<String> maybeString = Optional.of("foo");
    String newString = maybeString
            .map(this::runIfExist)
            .orElseGet(() -> runIfEmpty());
    System.out.println(newString);
}


If we now run the execute() method, we see the following:
only run if optional is filled
foo 

When using this implementation, we see that the supplier inside the orElseGet() is not executed. Intuitively, this is the behavior I would expect in general.

Conclusion

When we just use a map to assign a value to a variable, you will not have any problem using orElse() for the alternative flow. You just have to make sure that the code inside the orElse() is absolutely side effect free.

On the other hand, if you need to steer behavior based on an Optional, for instance creating a new user when a user is not found, you should use the orElseGet() construct. You probably don't want the your alternative flow to be executed anyway regardless of the value of the Optional.

Because of the nature of declarative programming, the mix-up is made very easily. Moreover, you will only see the result when you actually execute the code. So, of course, I can now advocate writing proper test code (and you should), but it is better to know the difference beforehand. The difference is subtle, but it can have a large impact if you are not careful.

Flow (web browser) Side effect (computer science) Console (video game CLI) Implementation Encapsulation (networking) Java (programming language) Strings Testing Construct (game engine)

Opinions expressed by DZone contributors are their own.

Trending

  • RBAC With API Gateway and Open Policy Agent (OPA)
  • How to Implement Istio in Multicloud and Multicluster
  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR
  • Tech Hiring: Trends, Predictions, and Strategies for Success

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: