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,952 ****
@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) {
while (entry != null) {
action.accept((K)entry.key, (V)entry.value);
entry = entry.next;
}
}
}
@Override
! public synchronized void replaceAll(
! BiFunction<? super K, ? super V, ? extends V> function) {
! Map.super.replaceAll(function);
}
@Override
public synchronized V putIfAbsent(K key, V value) {
Objects.requireNonNull(value);
--- 930,972 ----
@Override
public synchronized void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action); // explicit check required in case
// table is empty.
! 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) {
! 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,1066 ****
return newValue;
}
@Override
! public 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;
--- 1076,1086 ----
return newValue;
}
@Override
! 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,1095 ****
}
return null;
}
@Override
! public 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;
--- 1105,1115 ----
}
return null;
}
@Override
! 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,1130 ****
return newValue;
}
@Override
! public 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;
--- 1140,1150 ----
return newValue;
}
@Override
! 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;