8023306: Add replace() implementations to TreeMap
authormduigou
Wed, 21 Aug 2013 12:03:19 -0700
changeset 19572 e43d5c2e79ca
parent 19571 a5417c41a998
child 19573 0f94d21c60d7
8023306: Add replace() implementations to TreeMap Reviewed-by: psandoz, alanb, chegar, bpb
jdk/src/share/classes/java/util/TreeMap.java
--- a/jdk/src/share/classes/java/util/TreeMap.java	Wed Aug 21 17:19:46 2013 +0200
+++ b/jdk/src/share/classes/java/util/TreeMap.java	Wed Aug 21 12:03:19 2013 -0700
@@ -973,6 +973,27 @@
     }
 
     @Override
+    public 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 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;