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

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


 956      *         and this map uses natural ordering, or its comparator
 957      *         does not permit null keys
 958      * @throws IllegalArgumentException {@inheritDoc}
 959      */
 960     public SortedMap<K,V> headMap(K toKey) {
 961         return headMap(toKey, false);
 962     }
 963 
 964     /**
 965      * @throws ClassCastException       {@inheritDoc}
 966      * @throws NullPointerException if {@code fromKey} is null
 967      *         and this map uses natural ordering, or its comparator
 968      *         does not permit null keys
 969      * @throws IllegalArgumentException {@inheritDoc}
 970      */
 971     public SortedMap<K,V> tailMap(K fromKey) {
 972         return tailMap(fromKey, true);
 973     }
 974 
 975     @Override





















 976     public void forEach(BiConsumer<? super K, ? super V> action) {
 977         Objects.requireNonNull(action);
 978         int expectedModCount = modCount;
 979         for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
 980             action.accept(e.key, e.value);
 981 
 982             if (expectedModCount != modCount) {
 983                 throw new ConcurrentModificationException();
 984             }
 985         }
 986     }
 987 
 988     @Override
 989     public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
 990         Objects.requireNonNull(function);
 991         int expectedModCount = modCount;
 992 
 993         for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
 994             e.value = Objects.requireNonNull(function.apply(e.key, e.value));
 995 




 956      *         and this map uses natural ordering, or its comparator
 957      *         does not permit null keys
 958      * @throws IllegalArgumentException {@inheritDoc}
 959      */
 960     public SortedMap<K,V> headMap(K toKey) {
 961         return headMap(toKey, false);
 962     }
 963 
 964     /**
 965      * @throws ClassCastException       {@inheritDoc}
 966      * @throws NullPointerException if {@code fromKey} is null
 967      *         and this map uses natural ordering, or its comparator
 968      *         does not permit null keys
 969      * @throws IllegalArgumentException {@inheritDoc}
 970      */
 971     public SortedMap<K,V> tailMap(K fromKey) {
 972         return tailMap(fromKey, true);
 973     }
 974 
 975     @Override
 976     public boolean replace(K key, V oldValue, V newValue) {
 977         Entry<K,V> p = getEntry(key);
 978         if (p!=null && Objects.equals(oldValue, p.value)) {
 979             p.value = newValue;
 980             return true;
 981         }
 982         return false;
 983     }
 984 
 985     @Override
 986     public V replace(K key, V value) {
 987         Entry<K,V> p = getEntry(key);
 988         if (p!=null) {
 989             V oldValue = p.value;
 990             p.value = value;
 991             return oldValue;
 992         }
 993         return null;
 994     }
 995 
 996     @Override
 997     public void forEach(BiConsumer<? super K, ? super V> action) {
 998         Objects.requireNonNull(action);
 999         int expectedModCount = modCount;
1000         for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
1001             action.accept(e.key, e.value);
1002 
1003             if (expectedModCount != modCount) {
1004                 throw new ConcurrentModificationException();
1005             }
1006         }
1007     }
1008 
1009     @Override
1010     public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
1011         Objects.requireNonNull(function);
1012         int expectedModCount = modCount;
1013 
1014         for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
1015             e.value = Objects.requireNonNull(function.apply(e.key, e.value));
1016