< prev index next >
src/java.base/share/classes/java/util/HashMap.java
Print this page
@@ -1080,10 +1080,17 @@
return oldValue;
}
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<? super K, ? extends V> mappingFunction) {
if (mappingFunction == null)
throw new NullPointerException();
@@ -1113,11 +1120,13 @@
if (old != null && (oldValue = old.value) != null) {
afterNodeAccess(old);
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) {
old.value = v;
afterNodeAccess(old);
@@ -1128,11 +1137,11 @@
else {
tab[i] = newNode(hash, key, v, first);
if (binCount >= TREEIFY_THRESHOLD - 1)
treeifyBin(tab, hash);
}
- ++modCount;
+ modCount = mc + 1;
++size;
afterNodeInsertion(true);
return v;
}
< prev index next >