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

Print this page




 511      * Associates the specified value with the specified key in this map.
 512      * If the map previously contained a mapping for the key, the old
 513      * value is replaced.
 514      *
 515      * @param key key with which the specified value is to be associated
 516      * @param value value to be associated with the specified key
 517      *
 518      * @return the previous value associated with {@code key}, or
 519      *         {@code null} if there was no mapping for {@code key}.
 520      *         (A {@code null} return can also indicate that the map
 521      *         previously associated {@code null} with {@code key}.)
 522      * @throws ClassCastException if the specified key cannot be compared
 523      *         with the keys currently in the map
 524      * @throws NullPointerException if the specified key is null
 525      *         and this map uses natural ordering, or its comparator
 526      *         does not permit null keys
 527      */
 528     public V put(K key, V value) {
 529         Entry<K,V> t = root;
 530         if (t == null) {
 531             // TBD:
 532             // 5045147: (coll) Adding null to an empty TreeSet should
 533             // throw NullPointerException
 534             //
 535             // compare(key, key); // type check

 536             root = new Entry<>(key, value, null);
 537             size = 1;
 538             modCount++;
 539             return null;
 540         }
 541         int cmp;
 542         Entry<K,V> parent;
 543         // split comparator and comparable paths
 544         Comparator<? super K> cpr = comparator;
 545         if (cpr != null) {
 546             do {
 547                 parent = t;
 548                 cmp = cpr.compare(key, t.key);
 549                 if (cmp < 0)
 550                     t = t.left;
 551                 else if (cmp > 0)
 552                     t = t.right;
 553                 else
 554                     return t.setValue(value);
 555             } while (t != null);




 511      * Associates the specified value with the specified key in this map.
 512      * If the map previously contained a mapping for the key, the old
 513      * value is replaced.
 514      *
 515      * @param key key with which the specified value is to be associated
 516      * @param value value to be associated with the specified key
 517      *
 518      * @return the previous value associated with {@code key}, or
 519      *         {@code null} if there was no mapping for {@code key}.
 520      *         (A {@code null} return can also indicate that the map
 521      *         previously associated {@code null} with {@code key}.)
 522      * @throws ClassCastException if the specified key cannot be compared
 523      *         with the keys currently in the map
 524      * @throws NullPointerException if the specified key is null
 525      *         and this map uses natural ordering, or its comparator
 526      *         does not permit null keys
 527      */
 528     public V put(K key, V value) {
 529         Entry<K,V> t = root;
 530         if (t == null) {
 531             if (key == null) {
 532                 if (comparator == null) {
 533                     throw new NullPointerException();
 534                 }
 535                 comparator.compare(key, key); // type check
 536             }
 537             root = new Entry<>(key, value, null);
 538             size = 1;
 539             modCount++;
 540             return null;
 541         }
 542         int cmp;
 543         Entry<K,V> parent;
 544         // split comparator and comparable paths
 545         Comparator<? super K> cpr = comparator;
 546         if (cpr != null) {
 547             do {
 548                 parent = t;
 549                 cmp = cpr.compare(key, t.key);
 550                 if (cmp < 0)
 551                     t = t.left;
 552                 else if (cmp > 0)
 553                     t = t.right;
 554                 else
 555                     return t.setValue(value);
 556             } while (t != null);