--- old/src/java.base/share/classes/java/util/HashMap.java 2015-02-03 15:00:13.000000000 -0800 +++ new/src/java.base/share/classes/java/util/HashMap.java 2015-02-03 15:00:13.000000000 -0800 @@ -1082,6 +1082,13 @@ return null; } + /** + * {@inheritDoc} + * + * If the function itself causes a structural modification to the map, a + * ConcurrentModificationException will be thrown. As with iterators, this + * exception is thrown on a best-effort basis. + */ @Override public V computeIfAbsent(K key, Function mappingFunction) { @@ -1115,7 +1122,9 @@ return oldValue; } } + int mc = modCount; V v = mappingFunction.apply(key); + if (mc != modCount) { throw new ConcurrentModificationException(); } if (v == null) { return null; } else if (old != null) { @@ -1130,7 +1139,7 @@ if (binCount >= TREEIFY_THRESHOLD - 1) treeifyBin(tab, hash); } - ++modCount; + modCount = mc + 1; ++size; afterNodeInsertion(true); return v;