Hi,

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 HashMap.

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 example:

http://cr.openjdk.java.net/~bchristi/8071667/webrev.0/

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?

Thanks,
-Brent

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

Reply via email to