src/share/classes/java/util/Hashtable.java
Print this page
rev 7313 : 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap
Reviewed-by: forax, duigou
Contributed-by: Mike Duigou <mike.duigou@oracle.com>, Remi Forax <forax@univ-mlv.fr>
@@ -942,11 +942,20 @@
}
@Override
public synchronized void replaceAll(
BiFunction<? super K, ? super V, ? extends V> function) {
- Map.super.replaceAll(function);
+ Objects.requireNonNull(function); // explicit check required in case
+ // table is empty.
+ 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;
+ }
+ }
}
@Override
public synchronized V putIfAbsent(K key, V value) {
Objects.requireNonNull(value);
@@ -1056,11 +1065,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 +1094,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 +1129,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;