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

Print this page
rev 3503 : [mq]: cr5045147


 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             compare(key, key); // type (and possibly null) check
 532 



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