RFC: Adding ConcurrentModificationException for HashMap.computeIfAbsent(), and JDK-8071667

Brent Christian brent.christian at oracle.com
Wed Feb 4 00:01:51 UTC 2015


The code in bug 8071667 [1] passes a mappingFunction to 
computeIfAbsent() which itself put()s a sufficient number of additional 
entries into the HashMap to cause a resize/rehash.  As a result, 
computeIfAbsent() doesn't add the new entry at the proper place in the 

While one should not (mis)use the mappingFunction in this way, 
HashMap.computeIfAbsent() (and similar HashMap methods which accept 
Lambda expressions) could check for and throw a 
ConcurrentModificationException on a "best-effort" basis, similar to 
iterators.  This is already done in bulk operations HashMap.forEach() 
and HashMap.replaceAll().

I think it's also worth making mention of this in the JavaDoc.

Here's an example of what might be done, using computeIfAbsent() as an 


I would update HashMap and Hashtable.  It looks like 
ConcurrentHashMap.computeIfAbsent() already forbids such usage, stating 
that the computation "must not attempt to update any other mappings of 
this map."

Comments on this approach?


1. https://bugs.openjdk.java.net/browse/JDK-8071667
    "HashMap.computeIfAbsent() adds entry that HashMap.get() does not find."

More information about the core-libs-dev mailing list