< prev index next >

src/java.base/share/classes/java/util/HashMap.java

Print this page

        

*** 1080,1089 **** --- 1080,1096 ---- 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,1123 **** --- 1120,1132 ---- 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,1138 **** else { tab[i] = newNode(hash, key, v, first); if (binCount >= TREEIFY_THRESHOLD - 1) treeifyBin(tab, hash); } ! ++modCount; ++size; afterNodeInsertion(true); return v; } --- 1137,1147 ---- else { tab[i] = newNode(hash, key, v, first); if (binCount >= TREEIFY_THRESHOLD - 1) treeifyBin(tab, hash); } ! modCount = mc + 1; ++size; afterNodeInsertion(true); return v; }
< prev index next >