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

*** 25,34 **** --- 25,36 ---- package java.util; import java.io.*; import java.lang.reflect.Array; + import java.util.function.BiConsumer; + import java.util.function.BiFunction; import java.util.function.Consumer; /** * This class implements the <tt>Map</tt> interface with a hash table, using * reference-equality in place of object-equality when comparing keys (and
*** 1335,1344 **** --- 1337,1378 ---- } tab[i] = k; tab[i + 1] = value; } + @Override + public void forEach(BiConsumer<? super K, ? super V> action) { + Objects.requireNonNull(action); + int expectedModCount = modCount; + + Object[] t = table; + for (int index = 0; index < t.length; index += 2) { + if (t[index] != null) { + action.accept((K) unmaskNull(t[index]), (V) t[index + 1]); + } + + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + + @Override + public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { + Objects.requireNonNull(function); + int expectedModCount = modCount; + + Object[] t = table; + for (int index = 0; index < t.length; index += 2) { + if (t[index] != null) { + t[index + 1] = function.apply((K) unmaskNull(t[index]), (V) t[index + 1]); + } + + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } + } + /** * Similar form as array-based Spliterators, but skips blank elements, * and guestimates size as decreasing by half per split. */ static class IdentityHashMapSpliterator<K,V> {