HashMap computeIfAbsent() method in Java with Examples

The computeIfAbsent(Key, Function) method of HashMap class is used to compute value for a given key using the given mapping function, if key is not already associated with a value (or is mapped to null) and enter that computed value in Hashmap else null.

  • If mapping function of this method returns null, then no mapping is recorded for that key.
  • At time of computation if remapping function throws an exception, the exception is rethrown, and the no mapping is recorded.
  • During computation, modification this map using this method is not allowed.
  • This method will throw a ConcurrentModificationException if the remapping function modified this map during computation.
public V 
       computeIfAbsent(K key,
             Function<? super K, ? extends V> remappingFunction)
Parameters: This method accepts two parameters:
key : key for which we want to compute value using mapping.
remappingFunction : function to do the operation on value.
Returns: This method returns current (existing or computed) value associated with the specified key, or null if mapping returns null.
// Java program to demonstrate 
// computeIfAbsent(Key, Function) method. 

import java.util.*; 

public class GFG { 

    // Main method 
    public static void main(String[] args) 

        // create a HashMap and add some values 
        HashMap<String, Integer> map 
            = new HashMap<>(); 
        map.put("key1", 10000); 
        map.put("key2", 55000); 
        map.put("key3", 44300); 
        map.put("key4", 53200); 

        // print map details 
        System.out.println("HashMap:\n "
                           + map.toString()); 

        // provide value for new key which is absent 
        // using computeIfAbsent method 
                            k -> 2000 + 33000); 
                            k -> 2000 * 34); 

        // print new mapping 
        System.out.println("New HashMap:\n "
                           + map); 


 {key1=10000, key2=55000, key3=44300, key4=53200}
New HashMap:
 {key1=10000, key2=55000, key5=35000, key6=68000, key3=44300, key4=53200}

9 Replies to “HashMap computeIfAbsent() method in Java with Examples”

  1. Taxi moto line
    128 Rue la Boétie
    75008 Paris
    +33 6 51 612 712  

    Taxi moto paris

    hello!,I really like your writing very much! percentage
    we communicate extra approximately your post on AOL?

    I require a specialist in this space to resolve
    my problem. Maybe that’s you! Taking a look forward to look you.

  2. Thanks for your concepts. One thing we’ve noticed is banks along with financial institutions are aware of the spending practices of consumers while also understand that most of the people max out there their real credit cards around the getaways. They properly take advantage of this specific fact and commence flooding your current inbox along with snail-mail box using hundreds of no-interest APR card offers just after the holiday season comes to an end. Knowing that should you be like 98% of American community, you’ll rush at the possiblity to consolidate card debt and shift balances for 0 apr interest rates credit cards. iiihhjm https://headachemedi.com – what to take for Headache pain

  3. Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a bit, but instead of that, this is magnificent blog. A great read. I’ll certainly be back.

  4. What i don’t understood is actually how you’re not actually much more well-liked than you may be now. You are so intelligent. You realize thus considerably relating to this subject, made me personally consider it from so many varied angles. Its like men and women aren’t fascinated unless it’s one thing to do with Lady gaga! Your own stuffs excellent. Always maintain it up!

  5. I was wondering if you ever considered changing the layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better?

Leave a Reply

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