src/share/classes/java/util/Hashtable.java
Print this page
rev 7360 : 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap
Reviewed-by: forax, duigou, psandoz
Contributed-by: Mike Duigou <mike.duigou@oracle.com>, Remi Forax <forax@univ-mlv.fr>
@@ -930,23 +930,43 @@
@Override
public synchronized void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action); // explicit check required in case
// table is empty.
- Entry<?,?>[] tab = table;
- for (Entry<?,?> entry : tab) {
+ final int expectedModCount = modCount;
+
+ Entry<?, ?>[] tab = table;
+ for (Entry<?, ?> entry : tab) {
while (entry != null) {
action.accept((K)entry.key, (V)entry.value);
entry = entry.next;
+
+ if (expectedModCount != modCount) {
+ throw new ConcurrentModificationException();
+ }
}
}
}
@Override
- public synchronized void replaceAll(
- BiFunction<? super K, ? super V, ? extends V> function) {
- Map.super.replaceAll(function);
+ public synchronized void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
+ Objects.requireNonNull(function); // explicit check required in case
+ // table is empty.
+ final int expectedModCount = modCount;
+
+ Entry<K, V>[] tab = (Entry<K, V>[])table;
+ for (Entry<K, V> entry : tab) {
+ while (entry != null) {
+ entry.value = Objects.requireNonNull(
+ function.apply(entry.key, entry.value));
+ entry = entry.next;
+
+ if (expectedModCount != modCount) {
+ throw new ConcurrentModificationException();
+ }
+ }
+ }
}
@Override
public synchronized V putIfAbsent(K key, V value) {
Objects.requireNonNull(value);
@@ -1056,11 +1076,11 @@
return newValue;
}
@Override
- public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+ public synchronized V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Entry<?,?> tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
@@ -1085,11 +1105,11 @@
}
return null;
}
@Override
- public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
+ public synchronized V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Entry<?,?> tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
@@ -1120,11 +1140,11 @@
return newValue;
}
@Override
- public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
+ public synchronized V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Entry<?,?> tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;