src/share/classes/java/util/TreeMap.java

Print this page
rev 7896 : 8023306: Add replace() implementations to TreeMap
Reviewed-by: duke

*** 971,980 **** --- 971,1001 ---- public SortedMap<K,V> tailMap(K fromKey) { return tailMap(fromKey, true); } @Override + public synchronized boolean replace(K key, V oldValue, V newValue) { + Entry<K,V> p = getEntry(key); + if (p!=null && Objects.equals(oldValue, p.value)) { + p.value = newValue; + return true; + } + return false; + } + + @Override + public synchronized V replace(K key, V value) { + Entry<K,V> p = getEntry(key); + if (p!=null) { + V oldValue = p.value; + p.value = value; + return oldValue; + } + return null; + } + + @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);