1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.io.Serializable;
  29 import java.util.function.BiConsumer;
  30 import java.util.function.BiFunction;
  31 import java.util.function.Consumer;
  32 
  33 /**
  34  * A Red-Black tree based {@link NavigableMap} implementation.
  35  * The map is sorted according to the {@linkplain Comparable natural
  36  * ordering} of its keys, or by a {@link Comparator} provided at map
  37  * creation time, depending on which constructor is used.
  38  *
  39  * <p>This implementation provides guaranteed log(n) time cost for the
  40  * {@code containsKey}, {@code get}, {@code put} and {@code remove}
  41  * operations.  Algorithms are adaptations of those in Cormen, Leiserson, and
  42  * Rivest's <em>Introduction to Algorithms</em>.
  43  *
  44  * <p>Note that the ordering maintained by a tree map, like any sorted map, and
  45  * whether or not an explicit comparator is provided, must be <em>consistent
  46  * with {@code equals}</em> if this sorted map is to correctly implement the
  47  * {@code Map} interface.  (See {@code Comparable} or {@code Comparator} for a
  48  * precise definition of <em>consistent with equals</em>.)  This is so because
  49  * the {@code Map} interface is defined in terms of the {@code equals}
  50  * operation, but a sorted map performs all key comparisons using its {@code
  51  * compareTo} (or {@code compare}) method, so two keys that are deemed equal by
  52  * this method are, from the standpoint of the sorted map, equal.  The behavior
  53  * of a sorted map <em>is</em> well-defined even if its ordering is
  54  * inconsistent with {@code equals}; it just fails to obey the general contract
  55  * of the {@code Map} interface.
  56  *
  57  * <p><strong>Note that this implementation is not synchronized.</strong>
  58  * If multiple threads access a map concurrently, and at least one of the
  59  * threads modifies the map structurally, it <em>must</em> be synchronized
  60  * externally.  (A structural modification is any operation that adds or
  61  * deletes one or more mappings; merely changing the value associated
  62  * with an existing key is not a structural modification.)  This is
  63  * typically accomplished by synchronizing on some object that naturally
  64  * encapsulates the map.
  65  * If no such object exists, the map should be "wrapped" using the
  66  * {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap}
  67  * method.  This is best done at creation time, to prevent accidental
  68  * unsynchronized access to the map: <pre>
  69  *   SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));</pre>
  70  *
  71  * <p>The iterators returned by the {@code iterator} method of the collections
  72  * returned by all of this class's "collection view methods" are
  73  * <em>fail-fast</em>: if the map is structurally modified at any time after
  74  * the iterator is created, in any way except through the iterator's own
  75  * {@code remove} method, the iterator will throw a {@link
  76  * ConcurrentModificationException}.  Thus, in the face of concurrent
  77  * modification, the iterator fails quickly and cleanly, rather than risking
  78  * arbitrary, non-deterministic behavior at an undetermined time in the future.
  79  *
  80  * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
  81  * as it is, generally speaking, impossible to make any hard guarantees in the
  82  * presence of unsynchronized concurrent modification.  Fail-fast iterators
  83  * throw {@code ConcurrentModificationException} on a best-effort basis.
  84  * Therefore, it would be wrong to write a program that depended on this
  85  * exception for its correctness:   <em>the fail-fast behavior of iterators
  86  * should be used only to detect bugs.</em>
  87  *
  88  * <p>All {@code Map.Entry} pairs returned by methods in this class
  89  * and its views represent snapshots of mappings at the time they were
  90  * produced. They do <strong>not</strong> support the {@code Entry.setValue}
  91  * method. (Note however that it is possible to change mappings in the
  92  * associated map using {@code put}.)
  93  *
  94  * <p>This class is a member of the
  95  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
  96  * Java Collections Framework</a>.
  97  *
  98  * @param <K> the type of keys maintained by this map
  99  * @param <V> the type of mapped values
 100  *
 101  * @author  Josh Bloch and Doug Lea
 102  * @see Map
 103  * @see HashMap
 104  * @see Hashtable
 105  * @see Comparable
 106  * @see Comparator
 107  * @see Collection
 108  * @since 1.2
 109  */
 110 
 111 public class TreeMap<K,V>
 112     extends AbstractMap<K,V>
 113     implements NavigableMap<K,V>, Cloneable, java.io.Serializable
 114 {
 115     /**
 116      * The comparator used to maintain order in this tree map, or
 117      * null if it uses the natural ordering of its keys.
 118      *
 119      * @serial
 120      */
 121     private final Comparator<? super K> comparator;
 122 
 123     private transient Entry<K,V> root;
 124 
 125     /**
 126      * The number of entries in the tree
 127      */
 128     private transient int size = 0;
 129 
 130     /**
 131      * The number of structural modifications to the tree.
 132      */
 133     private transient int modCount = 0;
 134 
 135     /**
 136      * Constructs a new, empty tree map, using the natural ordering of its
 137      * keys.  All keys inserted into the map must implement the {@link
 138      * Comparable} interface.  Furthermore, all such keys must be
 139      * <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw
 140      * a {@code ClassCastException} for any keys {@code k1} and
 141      * {@code k2} in the map.  If the user attempts to put a key into the
 142      * map that violates this constraint (for example, the user attempts to
 143      * put a string key into a map whose keys are integers), the
 144      * {@code put(Object key, Object value)} call will throw a
 145      * {@code ClassCastException}.
 146      */
 147     public TreeMap() {
 148         comparator = null;
 149     }
 150 
 151     /**
 152      * Constructs a new, empty tree map, ordered according to the given
 153      * comparator.  All keys inserted into the map must be <em>mutually
 154      * comparable</em> by the given comparator: {@code comparator.compare(k1,
 155      * k2)} must not throw a {@code ClassCastException} for any keys
 156      * {@code k1} and {@code k2} in the map.  If the user attempts to put
 157      * a key into the map that violates this constraint, the {@code put(Object
 158      * key, Object value)} call will throw a
 159      * {@code ClassCastException}.
 160      *
 161      * @param comparator the comparator that will be used to order this map.
 162      *        If {@code null}, the {@linkplain Comparable natural
 163      *        ordering} of the keys will be used.
 164      */
 165     public TreeMap(Comparator<? super K> comparator) {
 166         this.comparator = comparator;
 167     }
 168 
 169     /**
 170      * Constructs a new tree map containing the same mappings as the given
 171      * map, ordered according to the <em>natural ordering</em> of its keys.
 172      * All keys inserted into the new map must implement the {@link
 173      * Comparable} interface.  Furthermore, all such keys must be
 174      * <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw
 175      * a {@code ClassCastException} for any keys {@code k1} and
 176      * {@code k2} in the map.  This method runs in n*log(n) time.
 177      *
 178      * @param  m the map whose mappings are to be placed in this map
 179      * @throws ClassCastException if the keys in m are not {@link Comparable},
 180      *         or are not mutually comparable
 181      * @throws NullPointerException if the specified map is null
 182      */
 183     public TreeMap(Map<? extends K, ? extends V> m) {
 184         comparator = null;
 185         putAll(m);
 186     }
 187 
 188     /**
 189      * Constructs a new tree map containing the same mappings and
 190      * using the same ordering as the specified sorted map.  This
 191      * method runs in linear time.
 192      *
 193      * @param  m the sorted map whose mappings are to be placed in this map,
 194      *         and whose comparator is to be used to sort this map
 195      * @throws NullPointerException if the specified map is null
 196      */
 197     public TreeMap(SortedMap<K, ? extends V> m) {
 198         comparator = m.comparator();
 199         try {
 200             buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
 201         } catch (java.io.IOException | ClassNotFoundException cannotHappen) {
 202         }
 203     }
 204 
 205 
 206     // Query Operations
 207 
 208     /**
 209      * Returns the number of key-value mappings in this map.
 210      *
 211      * @return the number of key-value mappings in this map
 212      */
 213     public int size() {
 214         return size;
 215     }
 216 
 217     /**
 218      * Returns {@code true} if this map contains a mapping for the specified
 219      * key.
 220      *
 221      * @param key key whose presence in this map is to be tested
 222      * @return {@code true} if this map contains a mapping for the
 223      *         specified key
 224      * @throws ClassCastException if the specified key cannot be compared
 225      *         with the keys currently in the map
 226      * @throws NullPointerException if the specified key is null
 227      *         and this map uses natural ordering, or its comparator
 228      *         does not permit null keys
 229      */
 230     public boolean containsKey(Object key) {
 231         return getEntry(key) != null;
 232     }
 233 
 234     /**
 235      * Returns {@code true} if this map maps one or more keys to the
 236      * specified value.  More formally, returns {@code true} if and only if
 237      * this map contains at least one mapping to a value {@code v} such
 238      * that {@code (value==null ? v==null : value.equals(v))}.  This
 239      * operation will probably require time linear in the map size for
 240      * most implementations.
 241      *
 242      * @param value value whose presence in this map is to be tested
 243      * @return {@code true} if a mapping to {@code value} exists;
 244      *         {@code false} otherwise
 245      * @since 1.2
 246      */
 247     public boolean containsValue(Object value) {
 248         for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e))
 249             if (valEquals(value, e.value))
 250                 return true;
 251         return false;
 252     }
 253 
 254     /**
 255      * Returns the value to which the specified key is mapped,
 256      * or {@code null} if this map contains no mapping for the key.
 257      *
 258      * <p>More formally, if this map contains a mapping from a key
 259      * {@code k} to a value {@code v} such that {@code key} compares
 260      * equal to {@code k} according to the map's ordering, then this
 261      * method returns {@code v}; otherwise it returns {@code null}.
 262      * (There can be at most one such mapping.)
 263      *
 264      * <p>A return value of {@code null} does not <em>necessarily</em>
 265      * indicate that the map contains no mapping for the key; it's also
 266      * possible that the map explicitly maps the key to {@code null}.
 267      * The {@link #containsKey containsKey} operation may be used to
 268      * distinguish these two cases.
 269      *
 270      * @throws ClassCastException if the specified key cannot be compared
 271      *         with the keys currently in the map
 272      * @throws NullPointerException if the specified key is null
 273      *         and this map uses natural ordering, or its comparator
 274      *         does not permit null keys
 275      */
 276     public V get(Object key) {
 277         Entry<K,V> p = getEntry(key);
 278         return (p==null ? null : p.value);
 279     }
 280 
 281     public Comparator<? super K> comparator() {
 282         return comparator;
 283     }
 284 
 285     /**
 286      * @throws NoSuchElementException {@inheritDoc}
 287      */
 288     public K firstKey() {
 289         return key(getFirstEntry());
 290     }
 291 
 292     /**
 293      * @throws NoSuchElementException {@inheritDoc}
 294      */
 295     public K lastKey() {
 296         return key(getLastEntry());
 297     }
 298 
 299     /**
 300      * Copies all of the mappings from the specified map to this map.
 301      * These mappings replace any mappings that this map had for any
 302      * of the keys currently in the specified map.
 303      *
 304      * @param  map mappings to be stored in this map
 305      * @throws ClassCastException if the class of a key or value in
 306      *         the specified map prevents it from being stored in this map
 307      * @throws NullPointerException if the specified map is null or
 308      *         the specified map contains a null key and this map does not
 309      *         permit null keys
 310      */
 311     public void putAll(Map<? extends K, ? extends V> map) {
 312         int mapSize = map.size();
 313         if (size==0 && mapSize!=0 && map instanceof SortedMap) {
 314             Comparator<?> c = ((SortedMap<?,?>)map).comparator();
 315             if (c == comparator || (c != null && c.equals(comparator))) {
 316                 ++modCount;
 317                 try {
 318                     buildFromSorted(mapSize, map.entrySet().iterator(),
 319                                     null, null);
 320                 } catch (java.io.IOException | ClassNotFoundException cannotHappen) {
 321                 }
 322                 return;
 323             }
 324         }
 325         super.putAll(map);
 326     }
 327 
 328     /**
 329      * Returns this map's entry for the given key, or {@code null} if the map
 330      * does not contain an entry for the key.
 331      *
 332      * @return this map's entry for the given key, or {@code null} if the map
 333      *         does not contain an entry for the key
 334      * @throws ClassCastException if the specified key cannot be compared
 335      *         with the keys currently in the map
 336      * @throws NullPointerException if the specified key is null
 337      *         and this map uses natural ordering, or its comparator
 338      *         does not permit null keys
 339      */
 340     final Entry<K,V> getEntry(Object key) {
 341         // Offload comparator-based version for sake of performance
 342         if (comparator != null)
 343             return getEntryUsingComparator(key);
 344         if (key == null)
 345             throw new NullPointerException();
 346         @SuppressWarnings("unchecked")
 347             Comparable<? super K> k = (Comparable<? super K>) key;
 348         Entry<K,V> p = root;
 349         while (p != null) {
 350             int cmp = k.compareTo(p.key);
 351             if (cmp < 0)
 352                 p = p.left;
 353             else if (cmp > 0)
 354                 p = p.right;
 355             else
 356                 return p;
 357         }
 358         return null;
 359     }
 360 
 361     /**
 362      * Version of getEntry using comparator. Split off from getEntry
 363      * for performance. (This is not worth doing for most methods,
 364      * that are less dependent on comparator performance, but is
 365      * worthwhile here.)
 366      */
 367     final Entry<K,V> getEntryUsingComparator(Object key) {
 368         @SuppressWarnings("unchecked")
 369             K k = (K) key;
 370         Comparator<? super K> cpr = comparator;
 371         if (cpr != null) {
 372             Entry<K,V> p = root;
 373             while (p != null) {
 374                 int cmp = cpr.compare(k, p.key);
 375                 if (cmp < 0)
 376                     p = p.left;
 377                 else if (cmp > 0)
 378                     p = p.right;
 379                 else
 380                     return p;
 381             }
 382         }
 383         return null;
 384     }
 385 
 386     /**
 387      * Gets the entry corresponding to the specified key; if no such entry
 388      * exists, returns the entry for the least key greater than the specified
 389      * key; if no such entry exists (i.e., the greatest key in the Tree is less
 390      * than the specified key), returns {@code null}.
 391      */
 392     final Entry<K,V> getCeilingEntry(K key) {
 393         Entry<K,V> p = root;
 394         while (p != null) {
 395             int cmp = compare(key, p.key);
 396             if (cmp < 0) {
 397                 if (p.left != null)
 398                     p = p.left;
 399                 else
 400                     return p;
 401             } else if (cmp > 0) {
 402                 if (p.right != null) {
 403                     p = p.right;
 404                 } else {
 405                     Entry<K,V> parent = p.parent;
 406                     Entry<K,V> ch = p;
 407                     while (parent != null && ch == parent.right) {
 408                         ch = parent;
 409                         parent = parent.parent;
 410                     }
 411                     return parent;
 412                 }
 413             } else
 414                 return p;
 415         }
 416         return null;
 417     }
 418 
 419     /**
 420      * Gets the entry corresponding to the specified key; if no such entry
 421      * exists, returns the entry for the greatest key less than the specified
 422      * key; if no such entry exists, returns {@code null}.
 423      */
 424     final Entry<K,V> getFloorEntry(K key) {
 425         Entry<K,V> p = root;
 426         while (p != null) {
 427             int cmp = compare(key, p.key);
 428             if (cmp > 0) {
 429                 if (p.right != null)
 430                     p = p.right;
 431                 else
 432                     return p;
 433             } else if (cmp < 0) {
 434                 if (p.left != null) {
 435                     p = p.left;
 436                 } else {
 437                     Entry<K,V> parent = p.parent;
 438                     Entry<K,V> ch = p;
 439                     while (parent != null && ch == parent.left) {
 440                         ch = parent;
 441                         parent = parent.parent;
 442                     }
 443                     return parent;
 444                 }
 445             } else
 446                 return p;
 447 
 448         }
 449         return null;
 450     }
 451 
 452     /**
 453      * Gets the entry for the least key greater than the specified
 454      * key; if no such entry exists, returns the entry for the least
 455      * key greater than the specified key; if no such entry exists
 456      * returns {@code null}.
 457      */
 458     final Entry<K,V> getHigherEntry(K key) {
 459         Entry<K,V> p = root;
 460         while (p != null) {
 461             int cmp = compare(key, p.key);
 462             if (cmp < 0) {
 463                 if (p.left != null)
 464                     p = p.left;
 465                 else
 466                     return p;
 467             } else {
 468                 if (p.right != null) {
 469                     p = p.right;
 470                 } else {
 471                     Entry<K,V> parent = p.parent;
 472                     Entry<K,V> ch = p;
 473                     while (parent != null && ch == parent.right) {
 474                         ch = parent;
 475                         parent = parent.parent;
 476                     }
 477                     return parent;
 478                 }
 479             }
 480         }
 481         return null;
 482     }
 483 
 484     /**
 485      * Returns the entry for the greatest key less than the specified key; if
 486      * no such entry exists (i.e., the least key in the Tree is greater than
 487      * the specified key), returns {@code null}.
 488      */
 489     final Entry<K,V> getLowerEntry(K key) {
 490         Entry<K,V> p = root;
 491         while (p != null) {
 492             int cmp = compare(key, p.key);
 493             if (cmp > 0) {
 494                 if (p.right != null)
 495                     p = p.right;
 496                 else
 497                     return p;
 498             } else {
 499                 if (p.left != null) {
 500                     p = p.left;
 501                 } else {
 502                     Entry<K,V> parent = p.parent;
 503                     Entry<K,V> ch = p;
 504                     while (parent != null && ch == parent.left) {
 505                         ch = parent;
 506                         parent = parent.parent;
 507                     }
 508                     return parent;
 509                 }
 510             }
 511         }
 512         return null;
 513     }
 514 
 515     /**
 516      * Associates the specified value with the specified key in this map.
 517      * If the map previously contained a mapping for the key, the old
 518      * value is replaced.
 519      *
 520      * @param key key with which the specified value is to be associated
 521      * @param value value to be associated with the specified key
 522      *
 523      * @return the previous value associated with {@code key}, or
 524      *         {@code null} if there was no mapping for {@code key}.
 525      *         (A {@code null} return can also indicate that the map
 526      *         previously associated {@code null} with {@code key}.)
 527      * @throws ClassCastException if the specified key cannot be compared
 528      *         with the keys currently in the map
 529      * @throws NullPointerException if the specified key is null
 530      *         and this map uses natural ordering, or its comparator
 531      *         does not permit null keys
 532      */
 533     public V put(K key, V value) {
 534         Entry<K,V> t = root;
 535         if (t == null) {
 536             compare(key, key); // type (and possibly null) check
 537 
 538             root = new Entry<>(key, value, null);
 539             size = 1;
 540             modCount++;
 541             return null;
 542         }
 543         int cmp;
 544         Entry<K,V> parent;
 545         // split comparator and comparable paths
 546         Comparator<? super K> cpr = comparator;
 547         if (cpr != null) {
 548             do {
 549                 parent = t;
 550                 cmp = cpr.compare(key, t.key);
 551                 if (cmp < 0)
 552                     t = t.left;
 553                 else if (cmp > 0)
 554                     t = t.right;
 555                 else
 556                     return t.setValue(value);
 557             } while (t != null);
 558         }
 559         else {
 560             if (key == null)
 561                 throw new NullPointerException();
 562             @SuppressWarnings("unchecked")
 563                 Comparable<? super K> k = (Comparable<? super K>) key;
 564             do {
 565                 parent = t;
 566                 cmp = k.compareTo(t.key);
 567                 if (cmp < 0)
 568                     t = t.left;
 569                 else if (cmp > 0)
 570                     t = t.right;
 571                 else
 572                     return t.setValue(value);
 573             } while (t != null);
 574         }
 575         Entry<K,V> e = new Entry<>(key, value, parent);
 576         if (cmp < 0)
 577             parent.left = e;
 578         else
 579             parent.right = e;
 580         fixAfterInsertion(e);
 581         size++;
 582         modCount++;
 583         return null;
 584     }
 585 
 586     /**
 587      * Removes the mapping for this key from this TreeMap if present.
 588      *
 589      * @param  key key for which mapping should be removed
 590      * @return the previous value associated with {@code key}, or
 591      *         {@code null} if there was no mapping for {@code key}.
 592      *         (A {@code null} return can also indicate that the map
 593      *         previously associated {@code null} with {@code key}.)
 594      * @throws ClassCastException if the specified key cannot be compared
 595      *         with the keys currently in the map
 596      * @throws NullPointerException if the specified key is null
 597      *         and this map uses natural ordering, or its comparator
 598      *         does not permit null keys
 599      */
 600     public V remove(Object key) {
 601         Entry<K,V> p = getEntry(key);
 602         if (p == null)
 603             return null;
 604 
 605         V oldValue = p.value;
 606         deleteEntry(p);
 607         return oldValue;
 608     }
 609 
 610     /**
 611      * Removes all of the mappings from this map.
 612      * The map will be empty after this call returns.
 613      */
 614     public void clear() {
 615         modCount++;
 616         size = 0;
 617         root = null;
 618     }
 619 
 620     /**
 621      * Returns a shallow copy of this {@code TreeMap} instance. (The keys and
 622      * values themselves are not cloned.)
 623      *
 624      * @return a shallow copy of this map
 625      */
 626     public Object clone() {
 627         TreeMap<?,?> clone;
 628         try {
 629             clone = (TreeMap<?,?>) super.clone();
 630         } catch (CloneNotSupportedException e) {
 631             throw new InternalError(e);
 632         }
 633 
 634         // Put clone into "virgin" state (except for comparator)
 635         clone.root = null;
 636         clone.size = 0;
 637         clone.modCount = 0;
 638         clone.entrySet = null;
 639         clone.navigableKeySet = null;
 640         clone.descendingMap = null;
 641 
 642         // Initialize clone with our mappings
 643         try {
 644             clone.buildFromSorted(size, entrySet().iterator(), null, null);
 645         } catch (java.io.IOException | ClassNotFoundException cannotHappen) {
 646         }
 647 
 648         return clone;
 649     }
 650 
 651     // NavigableMap API methods
 652 
 653     /**
 654      * @since 1.6
 655      */
 656     public Map.Entry<K,V> firstEntry() {
 657         return exportEntry(getFirstEntry());
 658     }
 659 
 660     /**
 661      * @since 1.6
 662      */
 663     public Map.Entry<K,V> lastEntry() {
 664         return exportEntry(getLastEntry());
 665     }
 666 
 667     /**
 668      * @since 1.6
 669      */
 670     public Map.Entry<K,V> pollFirstEntry() {
 671         Entry<K,V> p = getFirstEntry();
 672         Map.Entry<K,V> result = exportEntry(p);
 673         if (p != null)
 674             deleteEntry(p);
 675         return result;
 676     }
 677 
 678     /**
 679      * @since 1.6
 680      */
 681     public Map.Entry<K,V> pollLastEntry() {
 682         Entry<K,V> p = getLastEntry();
 683         Map.Entry<K,V> result = exportEntry(p);
 684         if (p != null)
 685             deleteEntry(p);
 686         return result;
 687     }
 688 
 689     /**
 690      * @throws ClassCastException {@inheritDoc}
 691      * @throws NullPointerException if the specified key is null
 692      *         and this map uses natural ordering, or its comparator
 693      *         does not permit null keys
 694      * @since 1.6
 695      */
 696     public Map.Entry<K,V> lowerEntry(K key) {
 697         return exportEntry(getLowerEntry(key));
 698     }
 699 
 700     /**
 701      * @throws ClassCastException {@inheritDoc}
 702      * @throws NullPointerException if the specified key is null
 703      *         and this map uses natural ordering, or its comparator
 704      *         does not permit null keys
 705      * @since 1.6
 706      */
 707     public K lowerKey(K key) {
 708         return keyOrNull(getLowerEntry(key));
 709     }
 710 
 711     /**
 712      * @throws ClassCastException {@inheritDoc}
 713      * @throws NullPointerException if the specified key is null
 714      *         and this map uses natural ordering, or its comparator
 715      *         does not permit null keys
 716      * @since 1.6
 717      */
 718     public Map.Entry<K,V> floorEntry(K key) {
 719         return exportEntry(getFloorEntry(key));
 720     }
 721 
 722     /**
 723      * @throws ClassCastException {@inheritDoc}
 724      * @throws NullPointerException if the specified key is null
 725      *         and this map uses natural ordering, or its comparator
 726      *         does not permit null keys
 727      * @since 1.6
 728      */
 729     public K floorKey(K key) {
 730         return keyOrNull(getFloorEntry(key));
 731     }
 732 
 733     /**
 734      * @throws ClassCastException {@inheritDoc}
 735      * @throws NullPointerException if the specified key is null
 736      *         and this map uses natural ordering, or its comparator
 737      *         does not permit null keys
 738      * @since 1.6
 739      */
 740     public Map.Entry<K,V> ceilingEntry(K key) {
 741         return exportEntry(getCeilingEntry(key));
 742     }
 743 
 744     /**
 745      * @throws ClassCastException {@inheritDoc}
 746      * @throws NullPointerException if the specified key is null
 747      *         and this map uses natural ordering, or its comparator
 748      *         does not permit null keys
 749      * @since 1.6
 750      */
 751     public K ceilingKey(K key) {
 752         return keyOrNull(getCeilingEntry(key));
 753     }
 754 
 755     /**
 756      * @throws ClassCastException {@inheritDoc}
 757      * @throws NullPointerException if the specified key is null
 758      *         and this map uses natural ordering, or its comparator
 759      *         does not permit null keys
 760      * @since 1.6
 761      */
 762     public Map.Entry<K,V> higherEntry(K key) {
 763         return exportEntry(getHigherEntry(key));
 764     }
 765 
 766     /**
 767      * @throws ClassCastException {@inheritDoc}
 768      * @throws NullPointerException if the specified key is null
 769      *         and this map uses natural ordering, or its comparator
 770      *         does not permit null keys
 771      * @since 1.6
 772      */
 773     public K higherKey(K key) {
 774         return keyOrNull(getHigherEntry(key));
 775     }
 776 
 777     // Views
 778 
 779     /**
 780      * Fields initialized to contain an instance of the entry set view
 781      * the first time this view is requested.  Views are stateless, so
 782      * there's no reason to create more than one.
 783      */
 784     private transient EntrySet entrySet;
 785     private transient KeySet<K> navigableKeySet;
 786     private transient NavigableMap<K,V> descendingMap;
 787 
 788     /**
 789      * Returns a {@link Set} view of the keys contained in this map.
 790      *
 791      * <p>The set's iterator returns the keys in ascending order.
 792      * The set's spliterator is
 793      * <em><a href="Spliterator.html#binding">late-binding</a></em>,
 794      * <em>fail-fast</em>, and additionally reports {@link Spliterator#SORTED}
 795      * and {@link Spliterator#ORDERED} with an encounter order that is ascending
 796      * key order.  The spliterator's comparator (see
 797      * {@link java.util.Spliterator#getComparator()}) is {@code null} if
 798      * the tree map's comparator (see {@link #comparator()}) is {@code null}.
 799      * Otherwise, the spliterator's comparator is the same as or imposes the
 800      * same total ordering as the tree map's comparator.
 801      *
 802      * <p>The set is backed by the map, so changes to the map are
 803      * reflected in the set, and vice-versa.  If the map is modified
 804      * while an iteration over the set is in progress (except through
 805      * the iterator's own {@code remove} operation), the results of
 806      * the iteration are undefined.  The set supports element removal,
 807      * which removes the corresponding mapping from the map, via the
 808      * {@code Iterator.remove}, {@code Set.remove},
 809      * {@code removeAll}, {@code retainAll}, and {@code clear}
 810      * operations.  It does not support the {@code add} or {@code addAll}
 811      * operations.
 812      */
 813     public Set<K> keySet() {
 814         return navigableKeySet();
 815     }
 816 
 817     /**
 818      * @since 1.6
 819      */
 820     public NavigableSet<K> navigableKeySet() {
 821         KeySet<K> nks = navigableKeySet;
 822         return (nks != null) ? nks : (navigableKeySet = new KeySet<>(this));
 823     }
 824 
 825     /**
 826      * @since 1.6
 827      */
 828     public NavigableSet<K> descendingKeySet() {
 829         return descendingMap().navigableKeySet();
 830     }
 831 
 832     /**
 833      * Returns a {@link Collection} view of the values contained in this map.
 834      *
 835      * <p>The collection's iterator returns the values in ascending order
 836      * of the corresponding keys. The collection's spliterator is
 837      * <em><a href="Spliterator.html#binding">late-binding</a></em>,
 838      * <em>fail-fast</em>, and additionally reports {@link Spliterator#ORDERED}
 839      * with an encounter order that is ascending order of the corresponding
 840      * keys.
 841      *
 842      * <p>The collection is backed by the map, so changes to the map are
 843      * reflected in the collection, and vice-versa.  If the map is
 844      * modified while an iteration over the collection is in progress
 845      * (except through the iterator's own {@code remove} operation),
 846      * the results of the iteration are undefined.  The collection
 847      * supports element removal, which removes the corresponding
 848      * mapping from the map, via the {@code Iterator.remove},
 849      * {@code Collection.remove}, {@code removeAll},
 850      * {@code retainAll} and {@code clear} operations.  It does not
 851      * support the {@code add} or {@code addAll} operations.
 852      */
 853     public Collection<V> values() {
 854         Collection<V> vs = values;
 855         if (vs == null) {
 856             vs = new Values();
 857             values = vs;
 858         }
 859         return vs;
 860     }
 861 
 862     /**
 863      * Returns a {@link Set} view of the mappings contained in this map.
 864      *
 865      * <p>The set's iterator returns the entries in ascending key order. The
 866      * set's spliterator is
 867      * <em><a href="Spliterator.html#binding">late-binding</a></em>,
 868      * <em>fail-fast</em>, and additionally reports {@link Spliterator#SORTED} and
 869      * {@link Spliterator#ORDERED} with an encounter order that is ascending key
 870      * order.
 871      *
 872      * <p>The set is backed by the map, so changes to the map are
 873      * reflected in the set, and vice-versa.  If the map is modified
 874      * while an iteration over the set is in progress (except through
 875      * the iterator's own {@code remove} operation, or through the
 876      * {@code setValue} operation on a map entry returned by the
 877      * iterator) the results of the iteration are undefined.  The set
 878      * supports element removal, which removes the corresponding
 879      * mapping from the map, via the {@code Iterator.remove},
 880      * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
 881      * {@code clear} operations.  It does not support the
 882      * {@code add} or {@code addAll} operations.
 883      */
 884     public Set<Map.Entry<K,V>> entrySet() {
 885         EntrySet es = entrySet;
 886         return (es != null) ? es : (entrySet = new EntrySet());
 887     }
 888 
 889     /**
 890      * @since 1.6
 891      */
 892     public NavigableMap<K, V> descendingMap() {
 893         NavigableMap<K, V> km = descendingMap;
 894         return (km != null) ? km :
 895             (descendingMap = new DescendingSubMap<>(this,
 896                                                     true, null, true,
 897                                                     true, null, true));
 898     }
 899 
 900     /**
 901      * @throws ClassCastException       {@inheritDoc}
 902      * @throws NullPointerException if {@code fromKey} or {@code toKey} is
 903      *         null and this map uses natural ordering, or its comparator
 904      *         does not permit null keys
 905      * @throws IllegalArgumentException {@inheritDoc}
 906      * @since 1.6
 907      */
 908     public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
 909                                     K toKey,   boolean toInclusive) {
 910         return new AscendingSubMap<>(this,
 911                                      false, fromKey, fromInclusive,
 912                                      false, toKey,   toInclusive);
 913     }
 914 
 915     /**
 916      * @throws ClassCastException       {@inheritDoc}
 917      * @throws NullPointerException if {@code toKey} is null
 918      *         and this map uses natural ordering, or its comparator
 919      *         does not permit null keys
 920      * @throws IllegalArgumentException {@inheritDoc}
 921      * @since 1.6
 922      */
 923     public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
 924         return new AscendingSubMap<>(this,
 925                                      true,  null,  true,
 926                                      false, toKey, inclusive);
 927     }
 928 
 929     /**
 930      * @throws ClassCastException       {@inheritDoc}
 931      * @throws NullPointerException if {@code fromKey} is null
 932      *         and this map uses natural ordering, or its comparator
 933      *         does not permit null keys
 934      * @throws IllegalArgumentException {@inheritDoc}
 935      * @since 1.6
 936      */
 937     public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
 938         return new AscendingSubMap<>(this,
 939                                      false, fromKey, inclusive,
 940                                      true,  null,    true);
 941     }
 942 
 943     /**
 944      * @throws ClassCastException       {@inheritDoc}
 945      * @throws NullPointerException if {@code fromKey} or {@code toKey} is
 946      *         null and this map uses natural ordering, or its comparator
 947      *         does not permit null keys
 948      * @throws IllegalArgumentException {@inheritDoc}
 949      */
 950     public SortedMap<K,V> subMap(K fromKey, K toKey) {
 951         return subMap(fromKey, true, toKey, false);
 952     }
 953 
 954     /**
 955      * @throws ClassCastException       {@inheritDoc}
 956      * @throws NullPointerException if {@code toKey} is null
 957      *         and this map uses natural ordering, or its comparator
 958      *         does not permit null keys
 959      * @throws IllegalArgumentException {@inheritDoc}
 960      */
 961     public SortedMap<K,V> headMap(K toKey) {
 962         return headMap(toKey, false);
 963     }
 964 
 965     /**
 966      * @throws ClassCastException       {@inheritDoc}
 967      * @throws NullPointerException if {@code fromKey} is null
 968      *         and this map uses natural ordering, or its comparator
 969      *         does not permit null keys
 970      * @throws IllegalArgumentException {@inheritDoc}
 971      */
 972     public SortedMap<K,V> tailMap(K fromKey) {
 973         return tailMap(fromKey, true);
 974     }
 975 
 976     @Override
 977     public boolean replace(K key, V oldValue, V newValue) {
 978         Entry<K,V> p = getEntry(key);
 979         if (p!=null && Objects.equals(oldValue, p.value)) {
 980             p.value = newValue;
 981             return true;
 982         }
 983         return false;
 984     }
 985 
 986     @Override
 987     public V replace(K key, V value) {
 988         Entry<K,V> p = getEntry(key);
 989         if (p!=null) {
 990             V oldValue = p.value;
 991             p.value = value;
 992             return oldValue;
 993         }
 994         return null;
 995     }
 996 
 997     @Override
 998     public void forEach(BiConsumer<? super K, ? super V> action) {
 999         Objects.requireNonNull(action);
1000         int expectedModCount = modCount;
1001         for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
1002             action.accept(e.key, e.value);
1003 
1004             if (expectedModCount != modCount) {
1005                 throw new ConcurrentModificationException();
1006             }
1007         }
1008     }
1009 
1010     @Override
1011     public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
1012         Objects.requireNonNull(function);
1013         int expectedModCount = modCount;
1014 
1015         for (Entry<K, V> e = getFirstEntry(); e != null; e = successor(e)) {
1016             e.value = function.apply(e.key, e.value);
1017 
1018             if (expectedModCount != modCount) {
1019                 throw new ConcurrentModificationException();
1020             }
1021         }
1022     }
1023 
1024     // View class support
1025 
1026     class Values extends AbstractCollection<V> {
1027         public Iterator<V> iterator() {
1028             return new ValueIterator(getFirstEntry());
1029         }
1030 
1031         public int size() {
1032             return TreeMap.this.size();
1033         }
1034 
1035         public boolean contains(Object o) {
1036             return TreeMap.this.containsValue(o);
1037         }
1038 
1039         public boolean remove(Object o) {
1040             for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) {
1041                 if (valEquals(e.getValue(), o)) {
1042                     deleteEntry(e);
1043                     return true;
1044                 }
1045             }
1046             return false;
1047         }
1048 
1049         public void clear() {
1050             TreeMap.this.clear();
1051         }
1052 
1053         public Spliterator<V> spliterator() {
1054             return new ValueSpliterator<>(TreeMap.this, null, null, 0, -1, 0);
1055         }
1056     }
1057 
1058     class EntrySet extends AbstractSet<Map.Entry<K,V>> {
1059         public Iterator<Map.Entry<K,V>> iterator() {
1060             return new EntryIterator(getFirstEntry());
1061         }
1062 
1063         public boolean contains(Object o) {
1064             if (!(o instanceof Map.Entry))
1065                 return false;
1066             Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
1067             Object value = entry.getValue();
1068             Entry<K,V> p = getEntry(entry.getKey());
1069             return p != null && valEquals(p.getValue(), value);
1070         }
1071 
1072         public boolean remove(Object o) {
1073             if (!(o instanceof Map.Entry))
1074                 return false;
1075             Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
1076             Object value = entry.getValue();
1077             Entry<K,V> p = getEntry(entry.getKey());
1078             if (p != null && valEquals(p.getValue(), value)) {
1079                 deleteEntry(p);
1080                 return true;
1081             }
1082             return false;
1083         }
1084 
1085         public int size() {
1086             return TreeMap.this.size();
1087         }
1088 
1089         public void clear() {
1090             TreeMap.this.clear();
1091         }
1092 
1093         public Spliterator<Map.Entry<K,V>> spliterator() {
1094             return new EntrySpliterator<>(TreeMap.this, null, null, 0, -1, 0);
1095         }
1096     }
1097 
1098     /*
1099      * Unlike Values and EntrySet, the KeySet class is static,
1100      * delegating to a NavigableMap to allow use by SubMaps, which
1101      * outweighs the ugliness of needing type-tests for the following
1102      * Iterator methods that are defined appropriately in main versus
1103      * submap classes.
1104      */
1105 
1106     Iterator<K> keyIterator() {
1107         return new KeyIterator(getFirstEntry());
1108     }
1109 
1110     Iterator<K> descendingKeyIterator() {
1111         return new DescendingKeyIterator(getLastEntry());
1112     }
1113 
1114     static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E> {
1115         private final NavigableMap<E, ?> m;
1116         KeySet(NavigableMap<E,?> map) { m = map; }
1117 
1118         public Iterator<E> iterator() {
1119             if (m instanceof TreeMap)
1120                 return ((TreeMap<E,?>)m).keyIterator();
1121             else
1122                 return ((TreeMap.NavigableSubMap<E,?>)m).keyIterator();
1123         }
1124 
1125         public Iterator<E> descendingIterator() {
1126             if (m instanceof TreeMap)
1127                 return ((TreeMap<E,?>)m).descendingKeyIterator();
1128             else
1129                 return ((TreeMap.NavigableSubMap<E,?>)m).descendingKeyIterator();
1130         }
1131 
1132         public int size() { return m.size(); }
1133         public boolean isEmpty() { return m.isEmpty(); }
1134         public boolean contains(Object o) { return m.containsKey(o); }
1135         public void clear() { m.clear(); }
1136         public E lower(E e) { return m.lowerKey(e); }
1137         public E floor(E e) { return m.floorKey(e); }
1138         public E ceiling(E e) { return m.ceilingKey(e); }
1139         public E higher(E e) { return m.higherKey(e); }
1140         public E first() { return m.firstKey(); }
1141         public E last() { return m.lastKey(); }
1142         public Comparator<? super E> comparator() { return m.comparator(); }
1143         public E pollFirst() {
1144             Map.Entry<E,?> e = m.pollFirstEntry();
1145             return (e == null) ? null : e.getKey();
1146         }
1147         public E pollLast() {
1148             Map.Entry<E,?> e = m.pollLastEntry();
1149             return (e == null) ? null : e.getKey();
1150         }
1151         public boolean remove(Object o) {
1152             int oldSize = size();
1153             m.remove(o);
1154             return size() != oldSize;
1155         }
1156         public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
1157                                       E toElement,   boolean toInclusive) {
1158             return new KeySet<>(m.subMap(fromElement, fromInclusive,
1159                                           toElement,   toInclusive));
1160         }
1161         public NavigableSet<E> headSet(E toElement, boolean inclusive) {
1162             return new KeySet<>(m.headMap(toElement, inclusive));
1163         }
1164         public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
1165             return new KeySet<>(m.tailMap(fromElement, inclusive));
1166         }
1167         public SortedSet<E> subSet(E fromElement, E toElement) {
1168             return subSet(fromElement, true, toElement, false);
1169         }
1170         public SortedSet<E> headSet(E toElement) {
1171             return headSet(toElement, false);
1172         }
1173         public SortedSet<E> tailSet(E fromElement) {
1174             return tailSet(fromElement, true);
1175         }
1176         public NavigableSet<E> descendingSet() {
1177             return new KeySet<>(m.descendingMap());
1178         }
1179 
1180         public Spliterator<E> spliterator() {
1181             return keySpliteratorFor(m);
1182         }
1183     }
1184 
1185     /**
1186      * Base class for TreeMap Iterators
1187      */
1188     abstract class PrivateEntryIterator<T> implements Iterator<T> {
1189         Entry<K,V> next;
1190         Entry<K,V> lastReturned;
1191         int expectedModCount;
1192 
1193         PrivateEntryIterator(Entry<K,V> first) {
1194             expectedModCount = modCount;
1195             lastReturned = null;
1196             next = first;
1197         }
1198 
1199         public final boolean hasNext() {
1200             return next != null;
1201         }
1202 
1203         final Entry<K,V> nextEntry() {
1204             Entry<K,V> e = next;
1205             if (e == null)
1206                 throw new NoSuchElementException();
1207             if (modCount != expectedModCount)
1208                 throw new ConcurrentModificationException();
1209             next = successor(e);
1210             lastReturned = e;
1211             return e;
1212         }
1213 
1214         final Entry<K,V> prevEntry() {
1215             Entry<K,V> e = next;
1216             if (e == null)
1217                 throw new NoSuchElementException();
1218             if (modCount != expectedModCount)
1219                 throw new ConcurrentModificationException();
1220             next = predecessor(e);
1221             lastReturned = e;
1222             return e;
1223         }
1224 
1225         public void remove() {
1226             if (lastReturned == null)
1227                 throw new IllegalStateException();
1228             if (modCount != expectedModCount)
1229                 throw new ConcurrentModificationException();
1230             // deleted entries are replaced by their successors
1231             if (lastReturned.left != null && lastReturned.right != null)
1232                 next = lastReturned;
1233             deleteEntry(lastReturned);
1234             expectedModCount = modCount;
1235             lastReturned = null;
1236         }
1237     }
1238 
1239     final class EntryIterator extends PrivateEntryIterator<Map.Entry<K,V>> {
1240         EntryIterator(Entry<K,V> first) {
1241             super(first);
1242         }
1243         public Map.Entry<K,V> next() {
1244             return nextEntry();
1245         }
1246     }
1247 
1248     final class ValueIterator extends PrivateEntryIterator<V> {
1249         ValueIterator(Entry<K,V> first) {
1250             super(first);
1251         }
1252         public V next() {
1253             return nextEntry().value;
1254         }
1255     }
1256 
1257     final class KeyIterator extends PrivateEntryIterator<K> {
1258         KeyIterator(Entry<K,V> first) {
1259             super(first);
1260         }
1261         public K next() {
1262             return nextEntry().key;
1263         }
1264     }
1265 
1266     final class DescendingKeyIterator extends PrivateEntryIterator<K> {
1267         DescendingKeyIterator(Entry<K,V> first) {
1268             super(first);
1269         }
1270         public K next() {
1271             return prevEntry().key;
1272         }
1273         public void remove() {
1274             if (lastReturned == null)
1275                 throw new IllegalStateException();
1276             if (modCount != expectedModCount)
1277                 throw new ConcurrentModificationException();
1278             deleteEntry(lastReturned);
1279             lastReturned = null;
1280             expectedModCount = modCount;
1281         }
1282     }
1283 
1284     // Little utilities
1285 
1286     /**
1287      * Compares two keys using the correct comparison method for this TreeMap.
1288      */
1289     @SuppressWarnings("unchecked")
1290     final int compare(Object k1, Object k2) {
1291         return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
1292             : comparator.compare((K)k1, (K)k2);
1293     }
1294 
1295     /**
1296      * Test two values for equality.  Differs from o1.equals(o2) only in
1297      * that it copes with {@code null} o1 properly.
1298      */
1299     static final boolean valEquals(Object o1, Object o2) {
1300         return (o1==null ? o2==null : o1.equals(o2));
1301     }
1302 
1303     /**
1304      * Return SimpleImmutableEntry for entry, or null if null
1305      */
1306     static <K,V> Map.Entry<K,V> exportEntry(TreeMap.Entry<K,V> e) {
1307         return (e == null) ? null :
1308             new AbstractMap.SimpleImmutableEntry<>(e);
1309     }
1310 
1311     /**
1312      * Return key for entry, or null if null
1313      */
1314     static <K,V> K keyOrNull(TreeMap.Entry<K,V> e) {
1315         return (e == null) ? null : e.key;
1316     }
1317 
1318     /**
1319      * Returns the key corresponding to the specified Entry.
1320      * @throws NoSuchElementException if the Entry is null
1321      */
1322     static <K> K key(Entry<K,?> e) {
1323         if (e==null)
1324             throw new NoSuchElementException();
1325         return e.key;
1326     }
1327 
1328 
1329     // SubMaps
1330 
1331     /**
1332      * Dummy value serving as unmatchable fence key for unbounded
1333      * SubMapIterators
1334      */
1335     private static final Object UNBOUNDED = new Object();
1336 
1337     /**
1338      * @serial include
1339      */
1340     abstract static class NavigableSubMap<K,V> extends AbstractMap<K,V>
1341         implements NavigableMap<K,V>, java.io.Serializable {
1342         @java.io.Serial
1343         private static final long serialVersionUID = -2102997345730753016L;
1344         /**
1345          * The backing map.
1346          */
1347         final TreeMap<K,V> m;
1348 
1349         /**
1350          * Endpoints are represented as triples (fromStart, lo,
1351          * loInclusive) and (toEnd, hi, hiInclusive). If fromStart is
1352          * true, then the low (absolute) bound is the start of the
1353          * backing map, and the other values are ignored. Otherwise,
1354          * if loInclusive is true, lo is the inclusive bound, else lo
1355          * is the exclusive bound. Similarly for the upper bound.
1356          */
1357         final K lo, hi;
1358         final boolean fromStart, toEnd;
1359         final boolean loInclusive, hiInclusive;
1360 
1361         NavigableSubMap(TreeMap<K,V> m,
1362                         boolean fromStart, K lo, boolean loInclusive,
1363                         boolean toEnd,     K hi, boolean hiInclusive) {
1364             if (!fromStart && !toEnd) {
1365                 if (m.compare(lo, hi) > 0)
1366                     throw new IllegalArgumentException("fromKey > toKey");
1367             } else {
1368                 if (!fromStart) // type check
1369                     m.compare(lo, lo);
1370                 if (!toEnd)
1371                     m.compare(hi, hi);
1372             }
1373 
1374             this.m = m;
1375             this.fromStart = fromStart;
1376             this.lo = lo;
1377             this.loInclusive = loInclusive;
1378             this.toEnd = toEnd;
1379             this.hi = hi;
1380             this.hiInclusive = hiInclusive;
1381         }
1382 
1383         // internal utilities
1384 
1385         final boolean tooLow(Object key) {
1386             if (!fromStart) {
1387                 int c = m.compare(key, lo);
1388                 if (c < 0 || (c == 0 && !loInclusive))
1389                     return true;
1390             }
1391             return false;
1392         }
1393 
1394         final boolean tooHigh(Object key) {
1395             if (!toEnd) {
1396                 int c = m.compare(key, hi);
1397                 if (c > 0 || (c == 0 && !hiInclusive))
1398                     return true;
1399             }
1400             return false;
1401         }
1402 
1403         final boolean inRange(Object key) {
1404             return !tooLow(key) && !tooHigh(key);
1405         }
1406 
1407         final boolean inClosedRange(Object key) {
1408             return (fromStart || m.compare(key, lo) >= 0)
1409                 && (toEnd || m.compare(hi, key) >= 0);
1410         }
1411 
1412         final boolean inRange(Object key, boolean inclusive) {
1413             return inclusive ? inRange(key) : inClosedRange(key);
1414         }
1415 
1416         /*
1417          * Absolute versions of relation operations.
1418          * Subclasses map to these using like-named "sub"
1419          * versions that invert senses for descending maps
1420          */
1421 
1422         final TreeMap.Entry<K,V> absLowest() {
1423             TreeMap.Entry<K,V> e =
1424                 (fromStart ?  m.getFirstEntry() :
1425                  (loInclusive ? m.getCeilingEntry(lo) :
1426                                 m.getHigherEntry(lo)));
1427             return (e == null || tooHigh(e.key)) ? null : e;
1428         }
1429 
1430         final TreeMap.Entry<K,V> absHighest() {
1431             TreeMap.Entry<K,V> e =
1432                 (toEnd ?  m.getLastEntry() :
1433                  (hiInclusive ?  m.getFloorEntry(hi) :
1434                                  m.getLowerEntry(hi)));
1435             return (e == null || tooLow(e.key)) ? null : e;
1436         }
1437 
1438         final TreeMap.Entry<K,V> absCeiling(K key) {
1439             if (tooLow(key))
1440                 return absLowest();
1441             TreeMap.Entry<K,V> e = m.getCeilingEntry(key);
1442             return (e == null || tooHigh(e.key)) ? null : e;
1443         }
1444 
1445         final TreeMap.Entry<K,V> absHigher(K key) {
1446             if (tooLow(key))
1447                 return absLowest();
1448             TreeMap.Entry<K,V> e = m.getHigherEntry(key);
1449             return (e == null || tooHigh(e.key)) ? null : e;
1450         }
1451 
1452         final TreeMap.Entry<K,V> absFloor(K key) {
1453             if (tooHigh(key))
1454                 return absHighest();
1455             TreeMap.Entry<K,V> e = m.getFloorEntry(key);
1456             return (e == null || tooLow(e.key)) ? null : e;
1457         }
1458 
1459         final TreeMap.Entry<K,V> absLower(K key) {
1460             if (tooHigh(key))
1461                 return absHighest();
1462             TreeMap.Entry<K,V> e = m.getLowerEntry(key);
1463             return (e == null || tooLow(e.key)) ? null : e;
1464         }
1465 
1466         /** Returns the absolute high fence for ascending traversal */
1467         final TreeMap.Entry<K,V> absHighFence() {
1468             return (toEnd ? null : (hiInclusive ?
1469                                     m.getHigherEntry(hi) :
1470                                     m.getCeilingEntry(hi)));
1471         }
1472 
1473         /** Return the absolute low fence for descending traversal  */
1474         final TreeMap.Entry<K,V> absLowFence() {
1475             return (fromStart ? null : (loInclusive ?
1476                                         m.getLowerEntry(lo) :
1477                                         m.getFloorEntry(lo)));
1478         }
1479 
1480         // Abstract methods defined in ascending vs descending classes
1481         // These relay to the appropriate absolute versions
1482 
1483         abstract TreeMap.Entry<K,V> subLowest();
1484         abstract TreeMap.Entry<K,V> subHighest();
1485         abstract TreeMap.Entry<K,V> subCeiling(K key);
1486         abstract TreeMap.Entry<K,V> subHigher(K key);
1487         abstract TreeMap.Entry<K,V> subFloor(K key);
1488         abstract TreeMap.Entry<K,V> subLower(K key);
1489 
1490         /** Returns ascending iterator from the perspective of this submap */
1491         abstract Iterator<K> keyIterator();
1492 
1493         abstract Spliterator<K> keySpliterator();
1494 
1495         /** Returns descending iterator from the perspective of this submap */
1496         abstract Iterator<K> descendingKeyIterator();
1497 
1498         // public methods
1499 
1500         public boolean isEmpty() {
1501             return (fromStart && toEnd) ? m.isEmpty() : entrySet().isEmpty();
1502         }
1503 
1504         public int size() {
1505             return (fromStart && toEnd) ? m.size() : entrySet().size();
1506         }
1507 
1508         public final boolean containsKey(Object key) {
1509             return inRange(key) && m.containsKey(key);
1510         }
1511 
1512         public final V put(K key, V value) {
1513             if (!inRange(key))
1514                 throw new IllegalArgumentException("key out of range");
1515             return m.put(key, value);
1516         }
1517 
1518         public final V get(Object key) {
1519             return !inRange(key) ? null :  m.get(key);
1520         }
1521 
1522         public final V remove(Object key) {
1523             return !inRange(key) ? null : m.remove(key);
1524         }
1525 
1526         public final Map.Entry<K,V> ceilingEntry(K key) {
1527             return exportEntry(subCeiling(key));
1528         }
1529 
1530         public final K ceilingKey(K key) {
1531             return keyOrNull(subCeiling(key));
1532         }
1533 
1534         public final Map.Entry<K,V> higherEntry(K key) {
1535             return exportEntry(subHigher(key));
1536         }
1537 
1538         public final K higherKey(K key) {
1539             return keyOrNull(subHigher(key));
1540         }
1541 
1542         public final Map.Entry<K,V> floorEntry(K key) {
1543             return exportEntry(subFloor(key));
1544         }
1545 
1546         public final K floorKey(K key) {
1547             return keyOrNull(subFloor(key));
1548         }
1549 
1550         public final Map.Entry<K,V> lowerEntry(K key) {
1551             return exportEntry(subLower(key));
1552         }
1553 
1554         public final K lowerKey(K key) {
1555             return keyOrNull(subLower(key));
1556         }
1557 
1558         public final K firstKey() {
1559             return key(subLowest());
1560         }
1561 
1562         public final K lastKey() {
1563             return key(subHighest());
1564         }
1565 
1566         public final Map.Entry<K,V> firstEntry() {
1567             return exportEntry(subLowest());
1568         }
1569 
1570         public final Map.Entry<K,V> lastEntry() {
1571             return exportEntry(subHighest());
1572         }
1573 
1574         public final Map.Entry<K,V> pollFirstEntry() {
1575             TreeMap.Entry<K,V> e = subLowest();
1576             Map.Entry<K,V> result = exportEntry(e);
1577             if (e != null)
1578                 m.deleteEntry(e);
1579             return result;
1580         }
1581 
1582         public final Map.Entry<K,V> pollLastEntry() {
1583             TreeMap.Entry<K,V> e = subHighest();
1584             Map.Entry<K,V> result = exportEntry(e);
1585             if (e != null)
1586                 m.deleteEntry(e);
1587             return result;
1588         }
1589 
1590         // Views
1591         transient NavigableMap<K,V> descendingMapView;
1592         transient EntrySetView entrySetView;
1593         transient KeySet<K> navigableKeySetView;
1594 
1595         public final NavigableSet<K> navigableKeySet() {
1596             KeySet<K> nksv = navigableKeySetView;
1597             return (nksv != null) ? nksv :
1598                 (navigableKeySetView = new TreeMap.KeySet<>(this));
1599         }
1600 
1601         public final Set<K> keySet() {
1602             return navigableKeySet();
1603         }
1604 
1605         public NavigableSet<K> descendingKeySet() {
1606             return descendingMap().navigableKeySet();
1607         }
1608 
1609         public final SortedMap<K,V> subMap(K fromKey, K toKey) {
1610             return subMap(fromKey, true, toKey, false);
1611         }
1612 
1613         public final SortedMap<K,V> headMap(K toKey) {
1614             return headMap(toKey, false);
1615         }
1616 
1617         public final SortedMap<K,V> tailMap(K fromKey) {
1618             return tailMap(fromKey, true);
1619         }
1620 
1621         // View classes
1622 
1623         abstract class EntrySetView extends AbstractSet<Map.Entry<K,V>> {
1624             private transient int size = -1, sizeModCount;
1625 
1626             public int size() {
1627                 if (fromStart && toEnd)
1628                     return m.size();
1629                 if (size == -1 || sizeModCount != m.modCount) {
1630                     sizeModCount = m.modCount;
1631                     size = 0;
1632                     Iterator<?> i = iterator();
1633                     while (i.hasNext()) {
1634                         size++;
1635                         i.next();
1636                     }
1637                 }
1638                 return size;
1639             }
1640 
1641             public boolean isEmpty() {
1642                 TreeMap.Entry<K,V> n = absLowest();
1643                 return n == null || tooHigh(n.key);
1644             }
1645 
1646             public boolean contains(Object o) {
1647                 if (!(o instanceof Map.Entry))
1648                     return false;
1649                 Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
1650                 Object key = entry.getKey();
1651                 if (!inRange(key))
1652                     return false;
1653                 TreeMap.Entry<?,?> node = m.getEntry(key);
1654                 return node != null &&
1655                     valEquals(node.getValue(), entry.getValue());
1656             }
1657 
1658             public boolean remove(Object o) {
1659                 if (!(o instanceof Map.Entry))
1660                     return false;
1661                 Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
1662                 Object key = entry.getKey();
1663                 if (!inRange(key))
1664                     return false;
1665                 TreeMap.Entry<K,V> node = m.getEntry(key);
1666                 if (node!=null && valEquals(node.getValue(),
1667                                             entry.getValue())) {
1668                     m.deleteEntry(node);
1669                     return true;
1670                 }
1671                 return false;
1672             }
1673         }
1674 
1675         /**
1676          * Iterators for SubMaps
1677          */
1678         abstract class SubMapIterator<T> implements Iterator<T> {
1679             TreeMap.Entry<K,V> lastReturned;
1680             TreeMap.Entry<K,V> next;
1681             final Object fenceKey;
1682             int expectedModCount;
1683 
1684             SubMapIterator(TreeMap.Entry<K,V> first,
1685                            TreeMap.Entry<K,V> fence) {
1686                 expectedModCount = m.modCount;
1687                 lastReturned = null;
1688                 next = first;
1689                 fenceKey = fence == null ? UNBOUNDED : fence.key;
1690             }
1691 
1692             public final boolean hasNext() {
1693                 return next != null && next.key != fenceKey;
1694             }
1695 
1696             final TreeMap.Entry<K,V> nextEntry() {
1697                 TreeMap.Entry<K,V> e = next;
1698                 if (e == null || e.key == fenceKey)
1699                     throw new NoSuchElementException();
1700                 if (m.modCount != expectedModCount)
1701                     throw new ConcurrentModificationException();
1702                 next = successor(e);
1703                 lastReturned = e;
1704                 return e;
1705             }
1706 
1707             final TreeMap.Entry<K,V> prevEntry() {
1708                 TreeMap.Entry<K,V> e = next;
1709                 if (e == null || e.key == fenceKey)
1710                     throw new NoSuchElementException();
1711                 if (m.modCount != expectedModCount)
1712                     throw new ConcurrentModificationException();
1713                 next = predecessor(e);
1714                 lastReturned = e;
1715                 return e;
1716             }
1717 
1718             final void removeAscending() {
1719                 if (lastReturned == null)
1720                     throw new IllegalStateException();
1721                 if (m.modCount != expectedModCount)
1722                     throw new ConcurrentModificationException();
1723                 // deleted entries are replaced by their successors
1724                 if (lastReturned.left != null && lastReturned.right != null)
1725                     next = lastReturned;
1726                 m.deleteEntry(lastReturned);
1727                 lastReturned = null;
1728                 expectedModCount = m.modCount;
1729             }
1730 
1731             final void removeDescending() {
1732                 if (lastReturned == null)
1733                     throw new IllegalStateException();
1734                 if (m.modCount != expectedModCount)
1735                     throw new ConcurrentModificationException();
1736                 m.deleteEntry(lastReturned);
1737                 lastReturned = null;
1738                 expectedModCount = m.modCount;
1739             }
1740 
1741         }
1742 
1743         final class SubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {
1744             SubMapEntryIterator(TreeMap.Entry<K,V> first,
1745                                 TreeMap.Entry<K,V> fence) {
1746                 super(first, fence);
1747             }
1748             public Map.Entry<K,V> next() {
1749                 return nextEntry();
1750             }
1751             public void remove() {
1752                 removeAscending();
1753             }
1754         }
1755 
1756         final class DescendingSubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {
1757             DescendingSubMapEntryIterator(TreeMap.Entry<K,V> last,
1758                                           TreeMap.Entry<K,V> fence) {
1759                 super(last, fence);
1760             }
1761 
1762             public Map.Entry<K,V> next() {
1763                 return prevEntry();
1764             }
1765             public void remove() {
1766                 removeDescending();
1767             }
1768         }
1769 
1770         // Implement minimal Spliterator as KeySpliterator backup
1771         final class SubMapKeyIterator extends SubMapIterator<K>
1772             implements Spliterator<K> {
1773             SubMapKeyIterator(TreeMap.Entry<K,V> first,
1774                               TreeMap.Entry<K,V> fence) {
1775                 super(first, fence);
1776             }
1777             public K next() {
1778                 return nextEntry().key;
1779             }
1780             public void remove() {
1781                 removeAscending();
1782             }
1783             public Spliterator<K> trySplit() {
1784                 return null;
1785             }
1786             public void forEachRemaining(Consumer<? super K> action) {
1787                 while (hasNext())
1788                     action.accept(next());
1789             }
1790             public boolean tryAdvance(Consumer<? super K> action) {
1791                 if (hasNext()) {
1792                     action.accept(next());
1793                     return true;
1794                 }
1795                 return false;
1796             }
1797             public long estimateSize() {
1798                 return Long.MAX_VALUE;
1799             }
1800             public int characteristics() {
1801                 return Spliterator.DISTINCT | Spliterator.ORDERED |
1802                     Spliterator.SORTED;
1803             }
1804             public final Comparator<? super K>  getComparator() {
1805                 return NavigableSubMap.this.comparator();
1806             }
1807         }
1808 
1809         final class DescendingSubMapKeyIterator extends SubMapIterator<K>
1810             implements Spliterator<K> {
1811             DescendingSubMapKeyIterator(TreeMap.Entry<K,V> last,
1812                                         TreeMap.Entry<K,V> fence) {
1813                 super(last, fence);
1814             }
1815             public K next() {
1816                 return prevEntry().key;
1817             }
1818             public void remove() {
1819                 removeDescending();
1820             }
1821             public Spliterator<K> trySplit() {
1822                 return null;
1823             }
1824             public void forEachRemaining(Consumer<? super K> action) {
1825                 while (hasNext())
1826                     action.accept(next());
1827             }
1828             public boolean tryAdvance(Consumer<? super K> action) {
1829                 if (hasNext()) {
1830                     action.accept(next());
1831                     return true;
1832                 }
1833                 return false;
1834             }
1835             public long estimateSize() {
1836                 return Long.MAX_VALUE;
1837             }
1838             public int characteristics() {
1839                 return Spliterator.DISTINCT | Spliterator.ORDERED;
1840             }
1841         }
1842     }
1843 
1844     /**
1845      * @serial include
1846      */
1847     static final class AscendingSubMap<K,V> extends NavigableSubMap<K,V> {
1848         @java.io.Serial
1849         private static final long serialVersionUID = 912986545866124060L;
1850 
1851         AscendingSubMap(TreeMap<K,V> m,
1852                         boolean fromStart, K lo, boolean loInclusive,
1853                         boolean toEnd,     K hi, boolean hiInclusive) {
1854             super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
1855         }
1856 
1857         public Comparator<? super K> comparator() {
1858             return m.comparator();
1859         }
1860 
1861         public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
1862                                         K toKey,   boolean toInclusive) {
1863             if (!inRange(fromKey, fromInclusive))
1864                 throw new IllegalArgumentException("fromKey out of range");
1865             if (!inRange(toKey, toInclusive))
1866                 throw new IllegalArgumentException("toKey out of range");
1867             return new AscendingSubMap<>(m,
1868                                          false, fromKey, fromInclusive,
1869                                          false, toKey,   toInclusive);
1870         }
1871 
1872         public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
1873             if (!inRange(toKey, inclusive))
1874                 throw new IllegalArgumentException("toKey out of range");
1875             return new AscendingSubMap<>(m,
1876                                          fromStart, lo,    loInclusive,
1877                                          false,     toKey, inclusive);
1878         }
1879 
1880         public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
1881             if (!inRange(fromKey, inclusive))
1882                 throw new IllegalArgumentException("fromKey out of range");
1883             return new AscendingSubMap<>(m,
1884                                          false, fromKey, inclusive,
1885                                          toEnd, hi,      hiInclusive);
1886         }
1887 
1888         public NavigableMap<K,V> descendingMap() {
1889             NavigableMap<K,V> mv = descendingMapView;
1890             return (mv != null) ? mv :
1891                 (descendingMapView =
1892                  new DescendingSubMap<>(m,
1893                                         fromStart, lo, loInclusive,
1894                                         toEnd,     hi, hiInclusive));
1895         }
1896 
1897         Iterator<K> keyIterator() {
1898             return new SubMapKeyIterator(absLowest(), absHighFence());
1899         }
1900 
1901         Spliterator<K> keySpliterator() {
1902             return new SubMapKeyIterator(absLowest(), absHighFence());
1903         }
1904 
1905         Iterator<K> descendingKeyIterator() {
1906             return new DescendingSubMapKeyIterator(absHighest(), absLowFence());
1907         }
1908 
1909         final class AscendingEntrySetView extends EntrySetView {
1910             public Iterator<Map.Entry<K,V>> iterator() {
1911                 return new SubMapEntryIterator(absLowest(), absHighFence());
1912             }
1913         }
1914 
1915         public Set<Map.Entry<K,V>> entrySet() {
1916             EntrySetView es = entrySetView;
1917             return (es != null) ? es : (entrySetView = new AscendingEntrySetView());
1918         }
1919 
1920         TreeMap.Entry<K,V> subLowest()       { return absLowest(); }
1921         TreeMap.Entry<K,V> subHighest()      { return absHighest(); }
1922         TreeMap.Entry<K,V> subCeiling(K key) { return absCeiling(key); }
1923         TreeMap.Entry<K,V> subHigher(K key)  { return absHigher(key); }
1924         TreeMap.Entry<K,V> subFloor(K key)   { return absFloor(key); }
1925         TreeMap.Entry<K,V> subLower(K key)   { return absLower(key); }
1926     }
1927 
1928     /**
1929      * @serial include
1930      */
1931     static final class DescendingSubMap<K,V>  extends NavigableSubMap<K,V> {
1932         @java.io.Serial
1933         private static final long serialVersionUID = 912986545866120460L;
1934         DescendingSubMap(TreeMap<K,V> m,
1935                         boolean fromStart, K lo, boolean loInclusive,
1936                         boolean toEnd,     K hi, boolean hiInclusive) {
1937             super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
1938         }
1939 
1940         private final Comparator<? super K> reverseComparator =
1941             Collections.reverseOrder(m.comparator);
1942 
1943         public Comparator<? super K> comparator() {
1944             return reverseComparator;
1945         }
1946 
1947         public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
1948                                         K toKey,   boolean toInclusive) {
1949             if (!inRange(fromKey, fromInclusive))
1950                 throw new IllegalArgumentException("fromKey out of range");
1951             if (!inRange(toKey, toInclusive))
1952                 throw new IllegalArgumentException("toKey out of range");
1953             return new DescendingSubMap<>(m,
1954                                           false, toKey,   toInclusive,
1955                                           false, fromKey, fromInclusive);
1956         }
1957 
1958         public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
1959             if (!inRange(toKey, inclusive))
1960                 throw new IllegalArgumentException("toKey out of range");
1961             return new DescendingSubMap<>(m,
1962                                           false, toKey, inclusive,
1963                                           toEnd, hi,    hiInclusive);
1964         }
1965 
1966         public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
1967             if (!inRange(fromKey, inclusive))
1968                 throw new IllegalArgumentException("fromKey out of range");
1969             return new DescendingSubMap<>(m,
1970                                           fromStart, lo, loInclusive,
1971                                           false, fromKey, inclusive);
1972         }
1973 
1974         public NavigableMap<K,V> descendingMap() {
1975             NavigableMap<K,V> mv = descendingMapView;
1976             return (mv != null) ? mv :
1977                 (descendingMapView =
1978                  new AscendingSubMap<>(m,
1979                                        fromStart, lo, loInclusive,
1980                                        toEnd,     hi, hiInclusive));
1981         }
1982 
1983         Iterator<K> keyIterator() {
1984             return new DescendingSubMapKeyIterator(absHighest(), absLowFence());
1985         }
1986 
1987         Spliterator<K> keySpliterator() {
1988             return new DescendingSubMapKeyIterator(absHighest(), absLowFence());
1989         }
1990 
1991         Iterator<K> descendingKeyIterator() {
1992             return new SubMapKeyIterator(absLowest(), absHighFence());
1993         }
1994 
1995         final class DescendingEntrySetView extends EntrySetView {
1996             public Iterator<Map.Entry<K,V>> iterator() {
1997                 return new DescendingSubMapEntryIterator(absHighest(), absLowFence());
1998             }
1999         }
2000 
2001         public Set<Map.Entry<K,V>> entrySet() {
2002             EntrySetView es = entrySetView;
2003             return (es != null) ? es : (entrySetView = new DescendingEntrySetView());
2004         }
2005 
2006         TreeMap.Entry<K,V> subLowest()       { return absHighest(); }
2007         TreeMap.Entry<K,V> subHighest()      { return absLowest(); }
2008         TreeMap.Entry<K,V> subCeiling(K key) { return absFloor(key); }
2009         TreeMap.Entry<K,V> subHigher(K key)  { return absLower(key); }
2010         TreeMap.Entry<K,V> subFloor(K key)   { return absCeiling(key); }
2011         TreeMap.Entry<K,V> subLower(K key)   { return absHigher(key); }
2012     }
2013 
2014     /**
2015      * This class exists solely for the sake of serialization
2016      * compatibility with previous releases of TreeMap that did not
2017      * support NavigableMap.  It translates an old-version SubMap into
2018      * a new-version AscendingSubMap. This class is never otherwise
2019      * used.
2020      *
2021      * @serial include
2022      */
2023     private class SubMap extends AbstractMap<K,V>
2024         implements SortedMap<K,V>, java.io.Serializable {
2025         @java.io.Serial
2026         private static final long serialVersionUID = -6520786458950516097L;
2027         private boolean fromStart = false, toEnd = false;
2028         private K fromKey, toKey;
2029         @java.io.Serial
2030         private Object readResolve() {
2031             return new AscendingSubMap<>(TreeMap.this,
2032                                          fromStart, fromKey, true,
2033                                          toEnd, toKey, false);
2034         }
2035         public Set<Map.Entry<K,V>> entrySet() { throw new InternalError(); }
2036         public K lastKey() { throw new InternalError(); }
2037         public K firstKey() { throw new InternalError(); }
2038         public SortedMap<K,V> subMap(K fromKey, K toKey) { throw new InternalError(); }
2039         public SortedMap<K,V> headMap(K toKey) { throw new InternalError(); }
2040         public SortedMap<K,V> tailMap(K fromKey) { throw new InternalError(); }
2041         public Comparator<? super K> comparator() { throw new InternalError(); }
2042     }
2043 
2044 
2045     // Red-black mechanics
2046 
2047     private static final boolean RED   = false;
2048     private static final boolean BLACK = true;
2049 
2050     /**
2051      * Node in the Tree.  Doubles as a means to pass key-value pairs back to
2052      * user (see Map.Entry).
2053      */
2054 
2055     static final class Entry<K,V> implements Map.Entry<K,V> {
2056         K key;
2057         V value;
2058         Entry<K,V> left;
2059         Entry<K,V> right;
2060         Entry<K,V> parent;
2061         boolean color = BLACK;
2062 
2063         /**
2064          * Make a new cell with given key, value, and parent, and with
2065          * {@code null} child links, and BLACK color.
2066          */
2067         Entry(K key, V value, Entry<K,V> parent) {
2068             this.key = key;
2069             this.value = value;
2070             this.parent = parent;
2071         }
2072 
2073         /**
2074          * Returns the key.
2075          *
2076          * @return the key
2077          */
2078         public K getKey() {
2079             return key;
2080         }
2081 
2082         /**
2083          * Returns the value associated with the key.
2084          *
2085          * @return the value associated with the key
2086          */
2087         public V getValue() {
2088             return value;
2089         }
2090 
2091         /**
2092          * Replaces the value currently associated with the key with the given
2093          * value.
2094          *
2095          * @return the value associated with the key before this method was
2096          *         called
2097          */
2098         public V setValue(V value) {
2099             V oldValue = this.value;
2100             this.value = value;
2101             return oldValue;
2102         }
2103 
2104         public boolean equals(Object o) {
2105             if (!(o instanceof Map.Entry))
2106                 return false;
2107             Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2108 
2109             return valEquals(key,e.getKey()) && valEquals(value,e.getValue());
2110         }
2111 
2112         public int hashCode() {
2113             int keyHash = (key==null ? 0 : key.hashCode());
2114             int valueHash = (value==null ? 0 : value.hashCode());
2115             return keyHash ^ valueHash;
2116         }
2117 
2118         public String toString() {
2119             return key + "=" + value;
2120         }
2121     }
2122 
2123     /**
2124      * Returns the first Entry in the TreeMap (according to the TreeMap's
2125      * key-sort function).  Returns null if the TreeMap is empty.
2126      */
2127     final Entry<K,V> getFirstEntry() {
2128         Entry<K,V> p = root;
2129         if (p != null)
2130             while (p.left != null)
2131                 p = p.left;
2132         return p;
2133     }
2134 
2135     /**
2136      * Returns the last Entry in the TreeMap (according to the TreeMap's
2137      * key-sort function).  Returns null if the TreeMap is empty.
2138      */
2139     final Entry<K,V> getLastEntry() {
2140         Entry<K,V> p = root;
2141         if (p != null)
2142             while (p.right != null)
2143                 p = p.right;
2144         return p;
2145     }
2146 
2147     /**
2148      * Returns the successor of the specified Entry, or null if no such.
2149      */
2150     static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {
2151         if (t == null)
2152             return null;
2153         else if (t.right != null) {
2154             Entry<K,V> p = t.right;
2155             while (p.left != null)
2156                 p = p.left;
2157             return p;
2158         } else {
2159             Entry<K,V> p = t.parent;
2160             Entry<K,V> ch = t;
2161             while (p != null && ch == p.right) {
2162                 ch = p;
2163                 p = p.parent;
2164             }
2165             return p;
2166         }
2167     }
2168 
2169     /**
2170      * Returns the predecessor of the specified Entry, or null if no such.
2171      */
2172     static <K,V> Entry<K,V> predecessor(Entry<K,V> t) {
2173         if (t == null)
2174             return null;
2175         else if (t.left != null) {
2176             Entry<K,V> p = t.left;
2177             while (p.right != null)
2178                 p = p.right;
2179             return p;
2180         } else {
2181             Entry<K,V> p = t.parent;
2182             Entry<K,V> ch = t;
2183             while (p != null && ch == p.left) {
2184                 ch = p;
2185                 p = p.parent;
2186             }
2187             return p;
2188         }
2189     }
2190 
2191     /**
2192      * Balancing operations.
2193      *
2194      * Implementations of rebalancings during insertion and deletion are
2195      * slightly different than the CLR version.  Rather than using dummy
2196      * nilnodes, we use a set of accessors that deal properly with null.  They
2197      * are used to avoid messiness surrounding nullness checks in the main
2198      * algorithms.
2199      */
2200 
2201     private static <K,V> boolean colorOf(Entry<K,V> p) {
2202         return (p == null ? BLACK : p.color);
2203     }
2204 
2205     private static <K,V> Entry<K,V> parentOf(Entry<K,V> p) {
2206         return (p == null ? null: p.parent);
2207     }
2208 
2209     private static <K,V> void setColor(Entry<K,V> p, boolean c) {
2210         if (p != null)
2211             p.color = c;
2212     }
2213 
2214     private static <K,V> Entry<K,V> leftOf(Entry<K,V> p) {
2215         return (p == null) ? null: p.left;
2216     }
2217 
2218     private static <K,V> Entry<K,V> rightOf(Entry<K,V> p) {
2219         return (p == null) ? null: p.right;
2220     }
2221 
2222     /** From CLR */
2223     private void rotateLeft(Entry<K,V> p) {
2224         if (p != null) {
2225             Entry<K,V> r = p.right;
2226             p.right = r.left;
2227             if (r.left != null)
2228                 r.left.parent = p;
2229             r.parent = p.parent;
2230             if (p.parent == null)
2231                 root = r;
2232             else if (p.parent.left == p)
2233                 p.parent.left = r;
2234             else
2235                 p.parent.right = r;
2236             r.left = p;
2237             p.parent = r;
2238         }
2239     }
2240 
2241     /** From CLR */
2242     private void rotateRight(Entry<K,V> p) {
2243         if (p != null) {
2244             Entry<K,V> l = p.left;
2245             p.left = l.right;
2246             if (l.right != null) l.right.parent = p;
2247             l.parent = p.parent;
2248             if (p.parent == null)
2249                 root = l;
2250             else if (p.parent.right == p)
2251                 p.parent.right = l;
2252             else p.parent.left = l;
2253             l.right = p;
2254             p.parent = l;
2255         }
2256     }
2257 
2258     /** From CLR */
2259     private void fixAfterInsertion(Entry<K,V> x) {
2260         x.color = RED;
2261 
2262         while (x != null && x != root && x.parent.color == RED) {
2263             if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
2264                 Entry<K,V> y = rightOf(parentOf(parentOf(x)));
2265                 if (colorOf(y) == RED) {
2266                     setColor(parentOf(x), BLACK);
2267                     setColor(y, BLACK);
2268                     setColor(parentOf(parentOf(x)), RED);
2269                     x = parentOf(parentOf(x));
2270                 } else {
2271                     if (x == rightOf(parentOf(x))) {
2272                         x = parentOf(x);
2273                         rotateLeft(x);
2274                     }
2275                     setColor(parentOf(x), BLACK);
2276                     setColor(parentOf(parentOf(x)), RED);
2277                     rotateRight(parentOf(parentOf(x)));
2278                 }
2279             } else {
2280                 Entry<K,V> y = leftOf(parentOf(parentOf(x)));
2281                 if (colorOf(y) == RED) {
2282                     setColor(parentOf(x), BLACK);
2283                     setColor(y, BLACK);
2284                     setColor(parentOf(parentOf(x)), RED);
2285                     x = parentOf(parentOf(x));
2286                 } else {
2287                     if (x == leftOf(parentOf(x))) {
2288                         x = parentOf(x);
2289                         rotateRight(x);
2290                     }
2291                     setColor(parentOf(x), BLACK);
2292                     setColor(parentOf(parentOf(x)), RED);
2293                     rotateLeft(parentOf(parentOf(x)));
2294                 }
2295             }
2296         }
2297         root.color = BLACK;
2298     }
2299 
2300     /**
2301      * Delete node p, and then rebalance the tree.
2302      */
2303     private void deleteEntry(Entry<K,V> p) {
2304         modCount++;
2305         size--;
2306 
2307         // If strictly internal, copy successor's element to p and then make p
2308         // point to successor.
2309         if (p.left != null && p.right != null) {
2310             Entry<K,V> s = successor(p);
2311             p.key = s.key;
2312             p.value = s.value;
2313             p = s;
2314         } // p has 2 children
2315 
2316         // Start fixup at replacement node, if it exists.
2317         Entry<K,V> replacement = (p.left != null ? p.left : p.right);
2318 
2319         if (replacement != null) {
2320             // Link replacement to parent
2321             replacement.parent = p.parent;
2322             if (p.parent == null)
2323                 root = replacement;
2324             else if (p == p.parent.left)
2325                 p.parent.left  = replacement;
2326             else
2327                 p.parent.right = replacement;
2328 
2329             // Null out links so they are OK to use by fixAfterDeletion.
2330             p.left = p.right = p.parent = null;
2331 
2332             // Fix replacement
2333             if (p.color == BLACK)
2334                 fixAfterDeletion(replacement);
2335         } else if (p.parent == null) { // return if we are the only node.
2336             root = null;
2337         } else { //  No children. Use self as phantom replacement and unlink.
2338             if (p.color == BLACK)
2339                 fixAfterDeletion(p);
2340 
2341             if (p.parent != null) {
2342                 if (p == p.parent.left)
2343                     p.parent.left = null;
2344                 else if (p == p.parent.right)
2345                     p.parent.right = null;
2346                 p.parent = null;
2347             }
2348         }
2349     }
2350 
2351     /** From CLR */
2352     private void fixAfterDeletion(Entry<K,V> x) {
2353         while (x != root && colorOf(x) == BLACK) {
2354             if (x == leftOf(parentOf(x))) {
2355                 Entry<K,V> sib = rightOf(parentOf(x));
2356 
2357                 if (colorOf(sib) == RED) {
2358                     setColor(sib, BLACK);
2359                     setColor(parentOf(x), RED);
2360                     rotateLeft(parentOf(x));
2361                     sib = rightOf(parentOf(x));
2362                 }
2363 
2364                 if (colorOf(leftOf(sib))  == BLACK &&
2365                     colorOf(rightOf(sib)) == BLACK) {
2366                     setColor(sib, RED);
2367                     x = parentOf(x);
2368                 } else {
2369                     if (colorOf(rightOf(sib)) == BLACK) {
2370                         setColor(leftOf(sib), BLACK);
2371                         setColor(sib, RED);
2372                         rotateRight(sib);
2373                         sib = rightOf(parentOf(x));
2374                     }
2375                     setColor(sib, colorOf(parentOf(x)));
2376                     setColor(parentOf(x), BLACK);
2377                     setColor(rightOf(sib), BLACK);
2378                     rotateLeft(parentOf(x));
2379                     x = root;
2380                 }
2381             } else { // symmetric
2382                 Entry<K,V> sib = leftOf(parentOf(x));
2383 
2384                 if (colorOf(sib) == RED) {
2385                     setColor(sib, BLACK);
2386                     setColor(parentOf(x), RED);
2387                     rotateRight(parentOf(x));
2388                     sib = leftOf(parentOf(x));
2389                 }
2390 
2391                 if (colorOf(rightOf(sib)) == BLACK &&
2392                     colorOf(leftOf(sib)) == BLACK) {
2393                     setColor(sib, RED);
2394                     x = parentOf(x);
2395                 } else {
2396                     if (colorOf(leftOf(sib)) == BLACK) {
2397                         setColor(rightOf(sib), BLACK);
2398                         setColor(sib, RED);
2399                         rotateLeft(sib);
2400                         sib = leftOf(parentOf(x));
2401                     }
2402                     setColor(sib, colorOf(parentOf(x)));
2403                     setColor(parentOf(x), BLACK);
2404                     setColor(leftOf(sib), BLACK);
2405                     rotateRight(parentOf(x));
2406                     x = root;
2407                 }
2408             }
2409         }
2410 
2411         setColor(x, BLACK);
2412     }
2413 
2414    @java.io.Serial
2415    private static final long serialVersionUID = 919286545866124006L;
2416 
2417     /**
2418      * Save the state of the {@code TreeMap} instance to a stream (i.e.,
2419      * serialize it).
2420      *
2421      * @serialData The <em>size</em> of the TreeMap (the number of key-value
2422      *             mappings) is emitted (int), followed by the key (Object)
2423      *             and value (Object) for each key-value mapping represented
2424      *             by the TreeMap. The key-value mappings are emitted in
2425      *             key-order (as determined by the TreeMap's Comparator,
2426      *             or by the keys' natural ordering if the TreeMap has no
2427      *             Comparator).
2428      */
2429     @java.io.Serial
2430     private void writeObject(java.io.ObjectOutputStream s)
2431         throws java.io.IOException {
2432         // Write out the Comparator and any hidden stuff
2433         s.defaultWriteObject();
2434 
2435         // Write out size (number of Mappings)
2436         s.writeInt(size);
2437 
2438         // Write out keys and values (alternating)
2439         for (Map.Entry<K, V> e : entrySet()) {
2440             s.writeObject(e.getKey());
2441             s.writeObject(e.getValue());
2442         }
2443     }
2444 
2445     /**
2446      * Reconstitute the {@code TreeMap} instance from a stream (i.e.,
2447      * deserialize it).
2448      */
2449     @java.io.Serial
2450     private void readObject(final java.io.ObjectInputStream s)
2451         throws java.io.IOException, ClassNotFoundException {
2452         // Read in the Comparator and any hidden stuff
2453         s.defaultReadObject();
2454 
2455         // Read in size
2456         int size = s.readInt();
2457 
2458         buildFromSorted(size, null, s, null);
2459     }
2460 
2461     /** Intended to be called only from TreeSet.readObject */
2462     void readTreeSet(int size, java.io.ObjectInputStream s, V defaultVal)
2463         throws java.io.IOException, ClassNotFoundException {
2464         buildFromSorted(size, null, s, defaultVal);
2465     }
2466 
2467     /** Intended to be called only from TreeSet.addAll */
2468     void addAllForTreeSet(SortedSet<? extends K> set, V defaultVal) {
2469         try {
2470             buildFromSorted(set.size(), set.iterator(), null, defaultVal);
2471         } catch (java.io.IOException | ClassNotFoundException cannotHappen) {
2472         }
2473     }
2474 
2475 
2476     /**
2477      * Linear time tree building algorithm from sorted data.  Can accept keys
2478      * and/or values from iterator or stream. This leads to too many
2479      * parameters, but seems better than alternatives.  The four formats
2480      * that this method accepts are:
2481      *
2482      *    1) An iterator of Map.Entries.  (it != null, defaultVal == null).
2483      *    2) An iterator of keys.         (it != null, defaultVal != null).
2484      *    3) A stream of alternating serialized keys and values.
2485      *                                   (it == null, defaultVal == null).
2486      *    4) A stream of serialized keys. (it == null, defaultVal != null).
2487      *
2488      * It is assumed that the comparator of the TreeMap is already set prior
2489      * to calling this method.
2490      *
2491      * @param size the number of keys (or key-value pairs) to be read from
2492      *        the iterator or stream
2493      * @param it If non-null, new entries are created from entries
2494      *        or keys read from this iterator.
2495      * @param str If non-null, new entries are created from keys and
2496      *        possibly values read from this stream in serialized form.
2497      *        Exactly one of it and str should be non-null.
2498      * @param defaultVal if non-null, this default value is used for
2499      *        each value in the map.  If null, each value is read from
2500      *        iterator or stream, as described above.
2501      * @throws java.io.IOException propagated from stream reads. This cannot
2502      *         occur if str is null.
2503      * @throws ClassNotFoundException propagated from readObject.
2504      *         This cannot occur if str is null.
2505      */
2506     private void buildFromSorted(int size, Iterator<?> it,
2507                                  java.io.ObjectInputStream str,
2508                                  V defaultVal)
2509         throws  java.io.IOException, ClassNotFoundException {
2510         this.size = size;
2511         root = buildFromSorted(0, 0, size-1, computeRedLevel(size),
2512                                it, str, defaultVal);
2513     }
2514 
2515     /**
2516      * Recursive "helper method" that does the real work of the
2517      * previous method.  Identically named parameters have
2518      * identical definitions.  Additional parameters are documented below.
2519      * It is assumed that the comparator and size fields of the TreeMap are
2520      * already set prior to calling this method.  (It ignores both fields.)
2521      *
2522      * @param level the current level of tree. Initial call should be 0.
2523      * @param lo the first element index of this subtree. Initial should be 0.
2524      * @param hi the last element index of this subtree.  Initial should be
2525      *        size-1.
2526      * @param redLevel the level at which nodes should be red.
2527      *        Must be equal to computeRedLevel for tree of this size.
2528      */
2529     @SuppressWarnings("unchecked")
2530     private final Entry<K,V> buildFromSorted(int level, int lo, int hi,
2531                                              int redLevel,
2532                                              Iterator<?> it,
2533                                              java.io.ObjectInputStream str,
2534                                              V defaultVal)
2535         throws  java.io.IOException, ClassNotFoundException {
2536         /*
2537          * Strategy: The root is the middlemost element. To get to it, we
2538          * have to first recursively construct the entire left subtree,
2539          * so as to grab all of its elements. We can then proceed with right
2540          * subtree.
2541          *
2542          * The lo and hi arguments are the minimum and maximum
2543          * indices to pull out of the iterator or stream for current subtree.
2544          * They are not actually indexed, we just proceed sequentially,
2545          * ensuring that items are extracted in corresponding order.
2546          */
2547 
2548         if (hi < lo) return null;
2549 
2550         int mid = (lo + hi) >>> 1;
2551 
2552         Entry<K,V> left  = null;
2553         if (lo < mid)
2554             left = buildFromSorted(level+1, lo, mid - 1, redLevel,
2555                                    it, str, defaultVal);
2556 
2557         // extract key and/or value from iterator or stream
2558         K key;
2559         V value;
2560         if (it != null) {
2561             if (defaultVal==null) {
2562                 Map.Entry<?,?> entry = (Map.Entry<?,?>)it.next();
2563                 key = (K)entry.getKey();
2564                 value = (V)entry.getValue();
2565             } else {
2566                 key = (K)it.next();
2567                 value = defaultVal;
2568             }
2569         } else { // use stream
2570             key = (K) str.readObject();
2571             value = (defaultVal != null ? defaultVal : (V) str.readObject());
2572         }
2573 
2574         Entry<K,V> middle =  new Entry<>(key, value, null);
2575 
2576         // color nodes in non-full bottommost level red
2577         if (level == redLevel)
2578             middle.color = RED;
2579 
2580         if (left != null) {
2581             middle.left = left;
2582             left.parent = middle;
2583         }
2584 
2585         if (mid < hi) {
2586             Entry<K,V> right = buildFromSorted(level+1, mid+1, hi, redLevel,
2587                                                it, str, defaultVal);
2588             middle.right = right;
2589             right.parent = middle;
2590         }
2591 
2592         return middle;
2593     }
2594 
2595     /**
2596      * Finds the level down to which to assign all nodes BLACK.  This is the
2597      * last `full' level of the complete binary tree produced by buildTree.
2598      * The remaining nodes are colored RED. (This makes a `nice' set of
2599      * color assignments wrt future insertions.) This level number is
2600      * computed by finding the number of splits needed to reach the zeroeth
2601      * node.
2602      *
2603      * @param size the (non-negative) number of keys in the tree to be built
2604      */
2605     private static int computeRedLevel(int size) {
2606         return 31 - Integer.numberOfLeadingZeros(size + 1);
2607     }
2608 
2609     /**
2610      * Currently, we support Spliterator-based versions only for the
2611      * full map, in either plain of descending form, otherwise relying
2612      * on defaults because size estimation for submaps would dominate
2613      * costs. The type tests needed to check these for key views are
2614      * not very nice but avoid disrupting existing class
2615      * structures. Callers must use plain default spliterators if this
2616      * returns null.
2617      */
2618     static <K> Spliterator<K> keySpliteratorFor(NavigableMap<K,?> m) {
2619         if (m instanceof TreeMap) {
2620             @SuppressWarnings("unchecked") TreeMap<K,Object> t =
2621                 (TreeMap<K,Object>) m;
2622             return t.keySpliterator();
2623         }
2624         if (m instanceof DescendingSubMap) {
2625             @SuppressWarnings("unchecked") DescendingSubMap<K,?> dm =
2626                 (DescendingSubMap<K,?>) m;
2627             TreeMap<K,?> tm = dm.m;
2628             if (dm == tm.descendingMap) {
2629                 @SuppressWarnings("unchecked") TreeMap<K,Object> t =
2630                     (TreeMap<K,Object>) tm;
2631                 return t.descendingKeySpliterator();
2632             }
2633         }
2634         @SuppressWarnings("unchecked") NavigableSubMap<K,?> sm =
2635             (NavigableSubMap<K,?>) m;
2636         return sm.keySpliterator();
2637     }
2638 
2639     final Spliterator<K> keySpliterator() {
2640         return new KeySpliterator<>(this, null, null, 0, -1, 0);
2641     }
2642 
2643     final Spliterator<K> descendingKeySpliterator() {
2644         return new DescendingKeySpliterator<>(this, null, null, 0, -2, 0);
2645     }
2646 
2647     /**
2648      * Base class for spliterators.  Iteration starts at a given
2649      * origin and continues up to but not including a given fence (or
2650      * null for end).  At top-level, for ascending cases, the first
2651      * split uses the root as left-fence/right-origin. From there,
2652      * right-hand splits replace the current fence with its left
2653      * child, also serving as origin for the split-off spliterator.
2654      * Left-hands are symmetric. Descending versions place the origin
2655      * at the end and invert ascending split rules.  This base class
2656      * is non-committal about directionality, or whether the top-level
2657      * spliterator covers the whole tree. This means that the actual
2658      * split mechanics are located in subclasses. Some of the subclass
2659      * trySplit methods are identical (except for return types), but
2660      * not nicely factorable.
2661      *
2662      * Currently, subclass versions exist only for the full map
2663      * (including descending keys via its descendingMap).  Others are
2664      * possible but currently not worthwhile because submaps require
2665      * O(n) computations to determine size, which substantially limits
2666      * potential speed-ups of using custom Spliterators versus default
2667      * mechanics.
2668      *
2669      * To boostrap initialization, external constructors use
2670      * negative size estimates: -1 for ascend, -2 for descend.
2671      */
2672     static class TreeMapSpliterator<K,V> {
2673         final TreeMap<K,V> tree;
2674         TreeMap.Entry<K,V> current; // traverser; initially first node in range
2675         TreeMap.Entry<K,V> fence;   // one past last, or null
2676         int side;                   // 0: top, -1: is a left split, +1: right
2677         int est;                    // size estimate (exact only for top-level)
2678         int expectedModCount;       // for CME checks
2679 
2680         TreeMapSpliterator(TreeMap<K,V> tree,
2681                            TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence,
2682                            int side, int est, int expectedModCount) {
2683             this.tree = tree;
2684             this.current = origin;
2685             this.fence = fence;
2686             this.side = side;
2687             this.est = est;
2688             this.expectedModCount = expectedModCount;
2689         }
2690 
2691         final int getEstimate() { // force initialization
2692             int s; TreeMap<K,V> t;
2693             if ((s = est) < 0) {
2694                 if ((t = tree) != null) {
2695                     current = (s == -1) ? t.getFirstEntry() : t.getLastEntry();
2696                     s = est = t.size;
2697                     expectedModCount = t.modCount;
2698                 }
2699                 else
2700                     s = est = 0;
2701             }
2702             return s;
2703         }
2704 
2705         public final long estimateSize() {
2706             return (long)getEstimate();
2707         }
2708     }
2709 
2710     static final class KeySpliterator<K,V>
2711         extends TreeMapSpliterator<K,V>
2712         implements Spliterator<K> {
2713         KeySpliterator(TreeMap<K,V> tree,
2714                        TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence,
2715                        int side, int est, int expectedModCount) {
2716             super(tree, origin, fence, side, est, expectedModCount);
2717         }
2718 
2719         public KeySpliterator<K,V> trySplit() {
2720             if (est < 0)
2721                 getEstimate(); // force initialization
2722             int d = side;
2723             TreeMap.Entry<K,V> e = current, f = fence,
2724                 s = ((e == null || e == f) ? null :      // empty
2725                      (d == 0)              ? tree.root : // was top
2726                      (d >  0)              ? e.right :   // was right
2727                      (d <  0 && f != null) ? f.left :    // was left
2728                      null);
2729             if (s != null && s != e && s != f &&
2730                 tree.compare(e.key, s.key) < 0) {        // e not already past s
2731                 side = 1;
2732                 return new KeySpliterator<>
2733                     (tree, e, current = s, -1, est >>>= 1, expectedModCount);
2734             }
2735             return null;
2736         }
2737 
2738         public void forEachRemaining(Consumer<? super K> action) {
2739             if (action == null)
2740                 throw new NullPointerException();
2741             if (est < 0)
2742                 getEstimate(); // force initialization
2743             TreeMap.Entry<K,V> f = fence, e, p, pl;
2744             if ((e = current) != null && e != f) {
2745                 current = f; // exhaust
2746                 do {
2747                     action.accept(e.key);
2748                     if ((p = e.right) != null) {
2749                         while ((pl = p.left) != null)
2750                             p = pl;
2751                     }
2752                     else {
2753                         while ((p = e.parent) != null && e == p.right)
2754                             e = p;
2755                     }
2756                 } while ((e = p) != null && e != f);
2757                 if (tree.modCount != expectedModCount)
2758                     throw new ConcurrentModificationException();
2759             }
2760         }
2761 
2762         public boolean tryAdvance(Consumer<? super K> action) {
2763             TreeMap.Entry<K,V> e;
2764             if (action == null)
2765                 throw new NullPointerException();
2766             if (est < 0)
2767                 getEstimate(); // force initialization
2768             if ((e = current) == null || e == fence)
2769                 return false;
2770             current = successor(e);
2771             action.accept(e.key);
2772             if (tree.modCount != expectedModCount)
2773                 throw new ConcurrentModificationException();
2774             return true;
2775         }
2776 
2777         public int characteristics() {
2778             return (side == 0 ? Spliterator.SIZED : 0) |
2779                 Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED;
2780         }
2781 
2782         public final Comparator<? super K>  getComparator() {
2783             return tree.comparator;
2784         }
2785 
2786     }
2787 
2788     static final class DescendingKeySpliterator<K,V>
2789         extends TreeMapSpliterator<K,V>
2790         implements Spliterator<K> {
2791         DescendingKeySpliterator(TreeMap<K,V> tree,
2792                                  TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence,
2793                                  int side, int est, int expectedModCount) {
2794             super(tree, origin, fence, side, est, expectedModCount);
2795         }
2796 
2797         public DescendingKeySpliterator<K,V> trySplit() {
2798             if (est < 0)
2799                 getEstimate(); // force initialization
2800             int d = side;
2801             TreeMap.Entry<K,V> e = current, f = fence,
2802                     s = ((e == null || e == f) ? null :      // empty
2803                          (d == 0)              ? tree.root : // was top
2804                          (d <  0)              ? e.left :    // was left
2805                          (d >  0 && f != null) ? f.right :   // was right
2806                          null);
2807             if (s != null && s != e && s != f &&
2808                 tree.compare(e.key, s.key) > 0) {       // e not already past s
2809                 side = 1;
2810                 return new DescendingKeySpliterator<>
2811                         (tree, e, current = s, -1, est >>>= 1, expectedModCount);
2812             }
2813             return null;
2814         }
2815 
2816         public void forEachRemaining(Consumer<? super K> action) {
2817             if (action == null)
2818                 throw new NullPointerException();
2819             if (est < 0)
2820                 getEstimate(); // force initialization
2821             TreeMap.Entry<K,V> f = fence, e, p, pr;
2822             if ((e = current) != null && e != f) {
2823                 current = f; // exhaust
2824                 do {
2825                     action.accept(e.key);
2826                     if ((p = e.left) != null) {
2827                         while ((pr = p.right) != null)
2828                             p = pr;
2829                     }
2830                     else {
2831                         while ((p = e.parent) != null && e == p.left)
2832                             e = p;
2833                     }
2834                 } while ((e = p) != null && e != f);
2835                 if (tree.modCount != expectedModCount)
2836                     throw new ConcurrentModificationException();
2837             }
2838         }
2839 
2840         public boolean tryAdvance(Consumer<? super K> action) {
2841             TreeMap.Entry<K,V> e;
2842             if (action == null)
2843                 throw new NullPointerException();
2844             if (est < 0)
2845                 getEstimate(); // force initialization
2846             if ((e = current) == null || e == fence)
2847                 return false;
2848             current = predecessor(e);
2849             action.accept(e.key);
2850             if (tree.modCount != expectedModCount)
2851                 throw new ConcurrentModificationException();
2852             return true;
2853         }
2854 
2855         public int characteristics() {
2856             return (side == 0 ? Spliterator.SIZED : 0) |
2857                 Spliterator.DISTINCT | Spliterator.ORDERED;
2858         }
2859     }
2860 
2861     static final class ValueSpliterator<K,V>
2862             extends TreeMapSpliterator<K,V>
2863             implements Spliterator<V> {
2864         ValueSpliterator(TreeMap<K,V> tree,
2865                          TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence,
2866                          int side, int est, int expectedModCount) {
2867             super(tree, origin, fence, side, est, expectedModCount);
2868         }
2869 
2870         public ValueSpliterator<K,V> trySplit() {
2871             if (est < 0)
2872                 getEstimate(); // force initialization
2873             int d = side;
2874             TreeMap.Entry<K,V> e = current, f = fence,
2875                     s = ((e == null || e == f) ? null :      // empty
2876                          (d == 0)              ? tree.root : // was top
2877                          (d >  0)              ? e.right :   // was right
2878                          (d <  0 && f != null) ? f.left :    // was left
2879                          null);
2880             if (s != null && s != e && s != f &&
2881                 tree.compare(e.key, s.key) < 0) {        // e not already past s
2882                 side = 1;
2883                 return new ValueSpliterator<>
2884                         (tree, e, current = s, -1, est >>>= 1, expectedModCount);
2885             }
2886             return null;
2887         }
2888 
2889         public void forEachRemaining(Consumer<? super V> action) {
2890             if (action == null)
2891                 throw new NullPointerException();
2892             if (est < 0)
2893                 getEstimate(); // force initialization
2894             TreeMap.Entry<K,V> f = fence, e, p, pl;
2895             if ((e = current) != null && e != f) {
2896                 current = f; // exhaust
2897                 do {
2898                     action.accept(e.value);
2899                     if ((p = e.right) != null) {
2900                         while ((pl = p.left) != null)
2901                             p = pl;
2902                     }
2903                     else {
2904                         while ((p = e.parent) != null && e == p.right)
2905                             e = p;
2906                     }
2907                 } while ((e = p) != null && e != f);
2908                 if (tree.modCount != expectedModCount)
2909                     throw new ConcurrentModificationException();
2910             }
2911         }
2912 
2913         public boolean tryAdvance(Consumer<? super V> action) {
2914             TreeMap.Entry<K,V> e;
2915             if (action == null)
2916                 throw new NullPointerException();
2917             if (est < 0)
2918                 getEstimate(); // force initialization
2919             if ((e = current) == null || e == fence)
2920                 return false;
2921             current = successor(e);
2922             action.accept(e.value);
2923             if (tree.modCount != expectedModCount)
2924                 throw new ConcurrentModificationException();
2925             return true;
2926         }
2927 
2928         public int characteristics() {
2929             return (side == 0 ? Spliterator.SIZED : 0) | Spliterator.ORDERED;
2930         }
2931     }
2932 
2933     static final class EntrySpliterator<K,V>
2934         extends TreeMapSpliterator<K,V>
2935         implements Spliterator<Map.Entry<K,V>> {
2936         EntrySpliterator(TreeMap<K,V> tree,
2937                          TreeMap.Entry<K,V> origin, TreeMap.Entry<K,V> fence,
2938                          int side, int est, int expectedModCount) {
2939             super(tree, origin, fence, side, est, expectedModCount);
2940         }
2941 
2942         public EntrySpliterator<K,V> trySplit() {
2943             if (est < 0)
2944                 getEstimate(); // force initialization
2945             int d = side;
2946             TreeMap.Entry<K,V> e = current, f = fence,
2947                     s = ((e == null || e == f) ? null :      // empty
2948                          (d == 0)              ? tree.root : // was top
2949                          (d >  0)              ? e.right :   // was right
2950                          (d <  0 && f != null) ? f.left :    // was left
2951                          null);
2952             if (s != null && s != e && s != f &&
2953                 tree.compare(e.key, s.key) < 0) {        // e not already past s
2954                 side = 1;
2955                 return new EntrySpliterator<>
2956                         (tree, e, current = s, -1, est >>>= 1, expectedModCount);
2957             }
2958             return null;
2959         }
2960 
2961         public void forEachRemaining(Consumer<? super Map.Entry<K, V>> action) {
2962             if (action == null)
2963                 throw new NullPointerException();
2964             if (est < 0)
2965                 getEstimate(); // force initialization
2966             TreeMap.Entry<K,V> f = fence, e, p, pl;
2967             if ((e = current) != null && e != f) {
2968                 current = f; // exhaust
2969                 do {
2970                     action.accept(e);
2971                     if ((p = e.right) != null) {
2972                         while ((pl = p.left) != null)
2973                             p = pl;
2974                     }
2975                     else {
2976                         while ((p = e.parent) != null && e == p.right)
2977                             e = p;
2978                     }
2979                 } while ((e = p) != null && e != f);
2980                 if (tree.modCount != expectedModCount)
2981                     throw new ConcurrentModificationException();
2982             }
2983         }
2984 
2985         public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
2986             TreeMap.Entry<K,V> e;
2987             if (action == null)
2988                 throw new NullPointerException();
2989             if (est < 0)
2990                 getEstimate(); // force initialization
2991             if ((e = current) == null || e == fence)
2992                 return false;
2993             current = successor(e);
2994             action.accept(e);
2995             if (tree.modCount != expectedModCount)
2996                 throw new ConcurrentModificationException();
2997             return true;
2998         }
2999 
3000         public int characteristics() {
3001             return (side == 0 ? Spliterator.SIZED : 0) |
3002                     Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED;
3003         }
3004 
3005         @Override
3006         public Comparator<Map.Entry<K, V>> getComparator() {
3007             // Adapt or create a key-based comparator
3008             if (tree.comparator != null) {
3009                 return Map.Entry.comparingByKey(tree.comparator);
3010             }
3011             else {
3012                 return (Comparator<Map.Entry<K, V>> & Serializable) (e1, e2) -> {
3013                     @SuppressWarnings("unchecked")
3014                     Comparable<? super K> k1 = (Comparable<? super K>) e1.getKey();
3015                     return k1.compareTo(e2.getKey());
3016                 };
3017             }
3018         }
3019     }
3020 }