From 8a3c65febe4da5102c544676429e26868e6110ee Mon Sep 17 00:00:00 2001 From: Mike Duigou Date: Wed, 21 Aug 2013 12:03:19 -0700 Subject: [PATCH] 8023306: Add replace() implementations to TreeMap Reviewed-by: psandoz, alanb, chegar, bpb --- jdk/src/share/classes/java/util/TreeMap.java | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/jdk/src/share/classes/java/util/TreeMap.java b/jdk/src/share/classes/java/util/TreeMap.java index 52d9df25a0b..9a4681d771a 100644 --- a/jdk/src/share/classes/java/util/TreeMap.java +++ b/jdk/src/share/classes/java/util/TreeMap.java @@ -972,6 +972,27 @@ public class TreeMap return tailMap(fromKey, true); } + @Override + public boolean replace(K key, V oldValue, V newValue) { + Entry 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 p = getEntry(key); + if (p!=null) { + V oldValue = p.value; + p.value = value; + return oldValue; + } + return null; + } + @Override public void forEach(BiConsumer action) { Objects.requireNonNull(action);