Codelytics

Visualise your geek speak

  • Blog
  • Terms
  • Privacy
  • Contact

Java 8: How To Retrieve Absent Values From HashMap

April 15, 2015 by Adrian Bartlett Leave a Comment

When attempting to retrieve a value from a Map which doesn’t exist, often it is desirable for the Map to provide an initial value. This saves the developer adding in extra logic to detect, create and insert the initial value. Java 8 adds a new Map method… java.util.Map::computeIfAbsent.

This is the second part in our java.util.Map series.  The first part is here: 8 Visual Steps to HashMap Enlightenment

java.util.Map::computeIfAbsent(K key, Function mappingFunction) has the following properties:

  • The second argument takes a function, which is called when there is no value for the key. The function is called to construct, insert and return the new value.
  • If a new value is constructed, this is performed atomically, so that the function will only ever be used at most once for each key

This method is particularly useful when used in a concurrent context i.e. java.util.concurrent.ConcurrentHashMap::computeIfAbsent. Before this method was available it was necessary to provide external synchronization, to ensure that overwrite problems didn’t occur. The following tutorial shows presents computeIfAbsent.

Step through computeIfAbsent in action

Click on the flashing arrow, to step through Codelytics’ visualization of computeIfAbsent.

Http iframes are not shown in https pages in many major browsers. Please read this post for details.

 

Do you like this Codelytics’ visualization?

If you like our visualizations, be sure to subscribe to our  mailing list below.

Do you like how this tutorial is presented?

The objective of this tutorial is to communicate Java concepts effectively, if you have any suggestions on how this could be improved or aspects you liked; leave a comment.

by clicking here

Share this:

  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Filed Under: Collections, Java Tagged With: Collections, Hashmap, Hashmap Tutorial, Java, java.util.Map series

Connect with us

  • Facebook
  • Google+
  • Twitter
  • YouTube

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Facebook
  • Google+
  • Twitter
  • YouTube

Subscribe to the blog

Recent posts

  • LinkedHashMap Tutorial
  • java.util.TreeMap visualized: interactive view from a new perspective
  • Constructor & Setter Dependency Injection
  • 4 Reasons to use a Java Semaphore
  • Java 8: How To Retrieve Absent Values From HashMap

Search

  • Blog
  • Terms
  • Privacy
  • Contact

All website content, including software, (excluding that owned by Wordpress and Centric Theme on Genesis Framework) is owned by Codelytics Pty Ltd and/or its affiliates, all rights reserved and protected by Australian copyright laws and relevant international treaty provisions. Any reproduction, redistribution and/or reverse engineering is expressly prohibited. For further details see the terms and conditions page on this site.
© Codelytics Pty Ltd, Adrian Bartlett, 2013

Copyright © 2022 · Centric Theme on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.