changeset 9704:fad3b6673159

8023306: Add replace() implementations to TreeMap Reviewed-by: psandoz, alanb, chegar, bpb
author mduigou
date Wed, 21 Aug 2013 12:03:19 -0700
parents 8996f47f738d
children 91a31c77be5b
files src/share/classes/java/util/TreeMap.java
diffstat 1 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/TreeMap.java	Wed Aug 21 17:19:46 2013 +0200
+++ b/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;