src/share/classes/java/util/WeakHashMap.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>

*** 26,35 **** --- 26,37 ---- package java.util; import java.lang.ref.WeakReference; import java.lang.ref.ReferenceQueue; import java.util.concurrent.ThreadLocalRandom; + import java.util.function.BiConsumer; + import java.util.function.BiFunction; import java.util.function.Consumer; /** * Hash table based implementation of the <tt>Map</tt> interface, with
*** 1034,1043 **** --- 1036,1087 ---- public Spliterator<Map.Entry<K,V>> spliterator() { return new EntrySpliterator<>(WeakHashMap.this, 0, -1, 0, 0); } } + @Override + public void forEach(BiConsumer<? super K, ? super V> action) { + Objects.requireNonNull(action); + int expectedModCount = modCount; + + Entry<K,V>[] tab = getTable(); + for(Entry<K,V> entry : tab) { + while(entry != null) { + Object key = entry.get(); + if (key != null) { + action.accept((K) WeakHashMap.unmaskNull(key), entry.value); + } + entry = entry.next; + + if (expectedModCount != modCount) { + throw new ConcurrentModificationException(); + } + } + } + } + + @Override + public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { + Objects.requireNonNull(function); + int expectedModCount = modCount; + + Entry<K,V>[] tab = getTable();; + for(Entry<K,V> entry : tab) { + while(entry != null) { + Object key = entry.get(); + if (key != null) { + entry.value = function.apply((K) WeakHashMap.unmaskNull(key), entry.value); + } + entry = entry.next; + } + + if (expectedModCount != modCount) { + throw new ConcurrentModificationException(); + } + } + } + /** * Similar form as other hash Spliterators, but skips dead * elements. */ static class WeakHashMapSpliterator<K,V> {