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

*** 23,32 **** --- 23,34 ---- * questions. */ package java.util; + import java.util.function.BiConsumer; + import java.util.function.BiFunction; import java.util.function.Consumer; /** * A Red-Black tree based {@link NavigableMap} implementation. * The map is sorted according to the {@linkplain Comparable natural
*** 943,952 **** --- 945,981 ---- */ public SortedMap<K,V> tailMap(K fromKey) { return tailMap(fromKey, true); } + @Override + public void forEach(BiConsumer<? super K, ? super V> action) { + Objects.requireNonNull(action); + int expectedModCount = modCount; + for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) { + action.accept(e.key, e.value); + + if (expectedModCount != modCount) { + throw new ConcurrentModificationException(); + } + } + } + + @Override + public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { + Objects.requireNonNull(function); + int expectedModCount = modCount; + + for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) { + e.value = Objects.requireNonNull(function.apply(e.key, e.value)); + + if (expectedModCount != modCount) { + throw new ConcurrentModificationException(); + } + } + } + // View class support class Values extends AbstractCollection<V> { public Iterator<V> iterator() { return new ValueIterator(getFirstEntry());