ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/TreeMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/TreeMap.java (file contents):
Revision 1.12 by jsr166, Mon May 2 21:44:01 2005 UTC vs.
Revision 1.24 by jsr166, Sat Sep 10 20:01:23 2005 UTC

# Line 6 | Line 6
6   */
7  
8   package java.util;
9 <
9 > import java.util.*; // for javadoc (till 6280605 is fixed)
10  
11   /**
12 < * Red-Black tree based implementation of the <tt>NavigableMap</tt> interface.
13 < * This class guarantees that the map will be in ascending key order, sorted
14 < * according to the <i>natural order</i> for the keys' class (see
15 < * <tt>Comparable</tt>), or by the comparator provided at creation time,
16 < * depending on which constructor is used.<p>
12 > * A Red-Black tree based {@link NavigableMap} implementation.
13 > * The map is sorted according to the {@linkplain Comparable natural
14 > * ordering} of its keys, or by a {@link Comparator} provided at map
15 > * creation time, depending on which constructor is used.
16   *
17 < * This implementation provides guaranteed log(n) time cost for the
17 > * <p>This implementation provides guaranteed log(n) time cost for the
18   * <tt>containsKey</tt>, <tt>get</tt>, <tt>put</tt> and <tt>remove</tt>
19   * operations.  Algorithms are adaptations of those in Cormen, Leiserson, and
20 < * Rivest's <I>Introduction to Algorithms</I>.<p>
20 > * Rivest's <I>Introduction to Algorithms</I>.
21   *
22 < * Note that the ordering maintained by a sorted map (whether or not an
22 > * <p>Note that the ordering maintained by a sorted map (whether or not an
23   * explicit comparator is provided) must be <i>consistent with equals</i> if
24   * this sorted map is to correctly implement the <tt>Map</tt> interface.  (See
25   * <tt>Comparable</tt> or <tt>Comparator</tt> for a precise definition of
# Line 30 | Line 29 | package java.util;
29   * method, so two keys that are deemed equal by this method are, from the
30   * standpoint of the sorted map, equal.  The behavior of a sorted map
31   * <i>is</i> well-defined even if its ordering is inconsistent with equals; it
32 < * just fails to obey the general contract of the <tt>Map</tt> interface.<p>
32 > * just fails to obey the general contract of the <tt>Map</tt> interface.
33   *
34 < * <b>Note that this implementation is not synchronized.</b> If multiple
35 < * threads access a map concurrently, and at least one of the threads modifies
36 < * the map structurally, it <i>must</i> be synchronized externally.  (A
37 < * structural modification is any operation that adds or deletes one or more
38 < * mappings; merely changing the value associated with an existing key is not
39 < * a structural modification.)  This is typically accomplished by
40 < * synchronizing on some object that naturally encapsulates the map.  If no
41 < * such object exists, the map should be "wrapped" using the
42 < * <tt>Collections.synchronizedMap</tt> method.  This is best done at creation
43 < * time, to prevent accidental unsynchronized access to the map:
44 < * <pre>
45 < *     Map m = Collections.synchronizedMap(new TreeMap(...));
46 < * </pre><p>
34 > * <p><strong>Note that this implementation is not synchronized.</strong>
35 > * If multiple threads access a map concurrently, and at least one of the
36 > * threads modifies the map structurally, it <i>must</i> be synchronized
37 > * externally.  (A structural modification is any operation that adds or
38 > * deletes one or more mappings; merely changing the value associated
39 > * with an existing key is not a structural modification.)  This is
40 > * typically accomplished by synchronizing on some object that naturally
41 > * encapsulates the map.
42 > * If no such object exists, the map should be "wrapped" using the
43 > * {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap}
44 > * method.  This is best done at creation time, to prevent accidental
45 > * unsynchronized access to the map: <pre>
46 > *   SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));</pre>
47   *
48 < * The iterators returned by all of this class's "collection view methods" are
48 > * <p>The iterators returned by the <tt>iterator</tt> method of the collections
49 > * returned by all of this class's "collection view methods" are
50   * <i>fail-fast</i>: if the map is structurally modified at any time after the
51   * iterator is created, in any way except through the iterator's own
52 < * <tt>remove</tt> or <tt>add</tt> methods, the iterator throws a
53 < * <tt>ConcurrentModificationException</tt>.  Thus, in the face of concurrent
52 > * <tt>remove</tt> method, the iterator will throw a {@link
53 > * ConcurrentModificationException}.  Thus, in the face of concurrent
54   * modification, the iterator fails quickly and cleanly, rather than risking
55 < * arbitrary, non-deterministic behavior at an undetermined time in the
56 < * future.
55 > * arbitrary, non-deterministic behavior at an undetermined time in the future.
56   *
57   * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
58   * as it is, generally speaking, impossible to make any hard guarantees in the
# Line 73 | Line 72 | package java.util;
72   * <a href="{@docRoot}/../guide/collections/index.html">
73   * Java Collections Framework</a>.
74   *
75 + * @param <K> the type of keys maintained by this map
76 + * @param <V> the type of mapped values
77 + *
78   * @author  Josh Bloch and Doug Lea
79   * @version %I%, %G%
80   * @see Map
# Line 81 | Line 83 | package java.util;
83   * @see Comparable
84   * @see Comparator
85   * @see Collection
84 * @see Collections#synchronizedMap(Map)
86   * @since 1.2
87   */
88  
# Line 90 | Line 91 | public class TreeMap<K,V>
91      implements NavigableMap<K,V>, Cloneable, java.io.Serializable
92   {
93      /**
94 <     * The Comparator used to maintain order in this TreeMap, or
95 <     * null if this TreeMap uses its elements natural ordering.
94 >     * The comparator used to maintain order in this tree map, or
95 >     * null if it uses the natural ordering of its keys.
96       *
97       * @serial
98       */
# Line 113 | Line 114 | public class TreeMap<K,V>
114      private void decrementSize()   { modCount++; size--; }
115  
116      /**
117 <     * Constructs a new, empty map, sorted according to the keys' natural
118 <     * order.  All keys inserted into the map must implement the
119 <     * <tt>Comparable</tt> interface.  Furthermore, all such keys must be
120 <     * <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw a
121 <     * ClassCastException for any elements <tt>k1</tt> and <tt>k2</tt> in the
122 <     * map.  If the user attempts to put a key into the map that violates this
123 <     * constraint (for example, the user attempts to put a string key into a
124 <     * map whose keys are integers), the <tt>put(Object key, Object
125 <     * value)</tt> call will throw a <tt>ClassCastException</tt>.
126 <     *
126 <     * @see Comparable
117 >     * Constructs a new, empty tree map, using the natural ordering of its
118 >     * keys.  All keys inserted into the map must implement the {@link
119 >     * Comparable} interface.  Furthermore, all such keys must be
120 >     * <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw
121 >     * a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and
122 >     * <tt>k2</tt> in the map.  If the user attempts to put a key into the
123 >     * map that violates this constraint (for example, the user attempts to
124 >     * put a string key into a map whose keys are integers), the
125 >     * <tt>put(Object key, Object value)</tt> call will throw a
126 >     * <tt>ClassCastException</tt>.
127       */
128      public TreeMap() {
129      }
130  
131      /**
132 <     * Constructs a new, empty map, sorted according to the given comparator.
133 <     * All keys inserted into the map must be <i>mutually comparable</i> by
134 <     * the given comparator: <tt>comparator.compare(k1, k2)</tt> must not
135 <     * throw a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and
136 <     * <tt>k2</tt> in the map.  If the user attempts to put a key into the
137 <     * map that violates this constraint, the <tt>put(Object key, Object
138 <     * value)</tt> call will throw a <tt>ClassCastException</tt>.
139 <     *
140 <     * @param c the comparator that will be used to sort this map.  A
141 <     *        <tt>null</tt> value indicates that the keys' <i>natural
142 <     *        ordering</i> should be used.
143 <     */
144 <    public TreeMap(Comparator<? super K> c) {
145 <        this.comparator = c;
132 >     * Constructs a new, empty tree map, ordered according to the given
133 >     * comparator.  All keys inserted into the map must be <i>mutually
134 >     * comparable</i> by the given comparator: <tt>comparator.compare(k1,
135 >     * k2)</tt> must not throw a <tt>ClassCastException</tt> for any keys
136 >     * <tt>k1</tt> and <tt>k2</tt> in the map.  If the user attempts to put
137 >     * a key into the map that violates this constraint, the <tt>put(Object
138 >     * key, Object value)</tt> call will throw a
139 >     * <tt>ClassCastException</tt>.
140 >     *
141 >     * @param comparator the comparator that will be used to order this map.
142 >     *        If <tt>null</tt>, the {@linkplain Comparable natural
143 >     *        ordering} of the keys will be used.
144 >     */
145 >    public TreeMap(Comparator<? super K> comparator) {
146 >        this.comparator = comparator;
147      }
148  
149      /**
150 <     * Constructs a new map containing the same mappings as the given map,
151 <     * sorted according to the keys' <i>natural order</i>.  All keys inserted
152 <     * into the new map must implement the <tt>Comparable</tt> interface.
153 <     * Furthermore, all such keys must be <i>mutually comparable</i>:
154 <     * <tt>k1.compareTo(k2)</tt> must not throw a <tt>ClassCastException</tt>
155 <     * for any elements <tt>k1</tt> and <tt>k2</tt> in the map.  This method
156 <     * runs in n*log(n) time.
157 <     *
158 <     * @param  m the map whose mappings are to be placed in this map.
159 <     * @throws ClassCastException the keys in t are not Comparable, or
160 <     *         are not mutually comparable.
161 <     * @throws NullPointerException if the specified map is null.
150 >     * Constructs a new tree map containing the same mappings as the given
151 >     * map, ordered according to the <i>natural ordering</i> of its keys.
152 >     * All keys inserted into the new map must implement the {@link
153 >     * Comparable} interface.  Furthermore, all such keys must be
154 >     * <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw
155 >     * a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and
156 >     * <tt>k2</tt> in the map.  This method runs in n*log(n) time.
157 >     *
158 >     * @param  m the map whose mappings are to be placed in this map
159 >     * @throws ClassCastException if the keys in m are not {@link Comparable},
160 >     *         or are not mutually comparable
161 >     * @throws NullPointerException if the specified map is null
162       */
163      public TreeMap(Map<? extends K, ? extends V> m) {
164          putAll(m);
165      }
166  
167      /**
168 <     * Constructs a new map containing the same mappings as the given
169 <     * <tt>SortedMap</tt>, sorted according to the same ordering.  This method
170 <     * runs in linear time.
168 >     * Constructs a new tree map containing the same mappings and
169 >     * using the same ordering as the specified sorted map.  This
170 >     * method runs in linear time.
171       *
172       * @param  m the sorted map whose mappings are to be placed in this map,
173 <     *         and whose comparator is to be used to sort this map.
174 <     * @throws NullPointerException if the specified sorted map is null.
173 >     *         and whose comparator is to be used to sort this map
174 >     * @throws NullPointerException if the specified map is null
175       */
176      public TreeMap(SortedMap<K, ? extends V> m) {
177          comparator = m.comparator();
# Line 187 | Line 188 | public class TreeMap<K,V>
188      /**
189       * Returns the number of key-value mappings in this map.
190       *
191 <     * @return the number of key-value mappings in this map.
191 >     * @return the number of key-value mappings in this map
192       */
193      public int size() {
194          return size;
# Line 197 | Line 198 | public class TreeMap<K,V>
198       * Returns <tt>true</tt> if this map contains a mapping for the specified
199       * key.
200       *
201 <     * @param key key whose presence in this map is to be tested.
201 <     *
201 >     * @param key key whose presence in this map is to be tested
202       * @return <tt>true</tt> if this map contains a mapping for the
203 <     *            specified key.
204 <     * @throws ClassCastException if the key cannot be compared with the keys
205 <     *                  currently in the map.
206 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
207 <     *                  natural ordering, or its comparator does not tolerate
208 <     *            <tt>null</tt> keys.
203 >     *         specified key
204 >     * @throws ClassCastException if the specified key cannot be compared
205 >     *         with the keys currently in the map
206 >     * @throws NullPointerException if the specified key is null
207 >     *         and this map uses natural ordering, or its comparator
208 >     *         does not permit null keys
209       */
210      public boolean containsKey(Object key) {
211          return getEntry(key) != null;
# Line 216 | Line 216 | public class TreeMap<K,V>
216       * specified value.  More formally, returns <tt>true</tt> if and only if
217       * this map contains at least one mapping to a value <tt>v</tt> such
218       * that <tt>(value==null ? v==null : value.equals(v))</tt>.  This
219 <     * operation will probably require time linear in the Map size for most
220 <     * implementations of Map.
219 >     * operation will probably require time linear in the map size for
220 >     * most implementations.
221       *
222 <     * @param value value whose presence in this Map is to be tested.
223 <     * @return  <tt>true</tt> if a mapping to <tt>value</tt> exists;
224 <     *          <tt>false</tt> otherwise.
222 >     * @param value value whose presence in this map is to be tested
223 >     * @return <tt>true</tt> if a mapping to <tt>value</tt> exists;
224 >     *         <tt>false</tt> otherwise
225       * @since 1.2
226       */
227      public boolean containsValue(Object value) {
# Line 250 | Line 250 | public class TreeMap<K,V>
250      }
251  
252      /**
253 <     * Returns the value to which this map maps the specified key.  Returns
254 <     * <tt>null</tt> if the map contains no mapping for this key.  A return
255 <     * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
256 <     * map contains no mapping for the key; it's also possible that the map
257 <     * explicitly maps the key to <tt>null</tt>.  The <tt>containsKey</tt>
258 <     * operation may be used to distinguish these two cases.
259 <     *
260 <     * @param key key whose associated value is to be returned.
261 <     * @return the value to which this map maps the specified key, or
262 <     *               <tt>null</tt> if the map contains no mapping for the key.
263 <     * @throws    ClassCastException if key cannot be compared with the keys
264 <     *                  currently in the map.
265 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
266 <     *                  natural ordering, or its comparator does not tolerate
267 <     *                  <tt>null</tt> keys.
253 >     * Returns the value to which the specified key is mapped,
254 >     * or {@code null} if this map contains no mapping for the key.
255       *
256 <     * @see #containsKey(Object)
256 >     * <p>More formally, if this map contains a mapping from a key
257 >     * {@code k} to a value {@code v} such that {@code key} compares
258 >     * equal to {@code k} according to the map's ordering, then this
259 >     * method returns {@code v}; otherwise it returns {@code null}.
260 >     * (There can be at most one such mapping.)
261 >     *
262 >     * <p>A return value of {@code null} does not <i>necessarily</i>
263 >     * indicate that the map contains no mapping for the key; it's also
264 >     * possible that the map explicitly maps the key to {@code null}.
265 >     * The {@link #containsKey containsKey} operation may be used to
266 >     * distinguish these two cases.
267 >     *
268 >     * @throws ClassCastException if the specified key cannot be compared
269 >     *         with the keys currently in the map
270 >     * @throws NullPointerException if the specified key is null
271 >     *         and this map uses natural ordering, or its comparator
272 >     *         does not permit null keys
273       */
274      public V get(Object key) {
275          Entry<K,V> p = getEntry(key);
276          return (p==null ? null : p.value);
277      }
278  
276    /**
277     * Returns the comparator used to order this map, or <tt>null</tt> if this
278     * map uses its keys' natural order.
279     *
280     * @return the comparator associated with this sorted map, or
281     *                <tt>null</tt> if it uses its keys' natural sort method.
282     */
279      public Comparator<? super K> comparator() {
280          return comparator;
281      }
282  
283      /**
284 <     * Returns the first (lowest) key currently in this sorted map.
289 <     *
290 <     * @return the first (lowest) key currently in this sorted map.
291 <     * @throws    NoSuchElementException Map is empty.
284 >     * @throws NoSuchElementException {@inheritDoc}
285       */
286      public K firstKey() {
287          return key(getFirstEntry());
288      }
289  
290      /**
291 <     * Returns the last (highest) key currently in this sorted map.
299 <     *
300 <     * @return the last (highest) key currently in this sorted map.
301 <     * @throws    NoSuchElementException Map is empty.
291 >     * @throws NoSuchElementException {@inheritDoc}
292       */
293      public K lastKey() {
294          return key(getLastEntry());
295      }
296  
297      /**
298 <     * Copies all of the mappings from the specified map to this map.  These
299 <     * mappings replace any mappings that this map had for any of the keys
300 <     * currently in the specified map.
301 <     *
302 <     * @param     map mappings to be stored in this map.
303 <     * @throws    ClassCastException class of a key or value in the specified
304 <     *                   map prevents it from being stored in this map.
305 <     *
306 <     * @throws NullPointerException if the given map is <tt>null</tt> or
307 <     *         this map does not permit <tt>null</tt> keys and a
318 <     *         key in the specified map is <tt>null</tt>.
298 >     * Copies all of the mappings from the specified map to this map.
299 >     * These mappings replace any mappings that this map had for any
300 >     * of the keys currently in the specified map.
301 >     *
302 >     * @param  map mappings to be stored in this map
303 >     * @throws ClassCastException if the class of a key or value in
304 >     *         the specified map prevents it from being stored in this map
305 >     * @throws NullPointerException if the specified map is null or
306 >     *         the specified map contains a null key and this map does not
307 >     *         permit null keys
308       */
309      public void putAll(Map<? extends K, ? extends V> map) {
310          int mapSize = map.size();
# Line 340 | Line 329 | public class TreeMap<K,V>
329       * does not contain an entry for the key.
330       *
331       * @return this map's entry for the given key, or <tt>null</tt> if the map
332 <     *                does not contain an entry for the key.
333 <     * @throws ClassCastException if the key cannot be compared with the keys
334 <     *                  currently in the map.
335 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
336 <     *                  natural order, or its comparator does not tolerate *
337 <     *                  <tt>null</tt> keys.
332 >     *         does not contain an entry for the key
333 >     * @throws ClassCastException if the specified key cannot be compared
334 >     *         with the keys currently in the map
335 >     * @throws NullPointerException if the specified key is null
336 >     *         and this map uses natural ordering, or its comparator
337 >     *         does not permit null keys
338       */
339      private Entry<K,V> getEntry(Object key) {
340          // Offload comparator-based version for sake of performance
# Line 525 | Line 514 | public class TreeMap<K,V>
514      }
515  
516      /**
517 <     * Returns the key corresponding to the specified Entry.  Throw
518 <     * NoSuchElementException if the Entry is <tt>null</tt>.
517 >     * Returns the key corresponding to the specified Entry.
518 >     * @throws NoSuchElementException if the Entry is null
519       */
520      private static <K> K key(Entry<K,?> e) {
521          if (e==null)
# Line 536 | Line 525 | public class TreeMap<K,V>
525  
526      /**
527       * Associates the specified value with the specified key in this map.
528 <     * If the map previously contained a mapping for this key, the old
528 >     * If the map previously contained a mapping for the key, the old
529       * value is replaced.
530       *
531 <     * @param key key with which the specified value is to be associated.
532 <     * @param value value to be associated with the specified key.
531 >     * @param key key with which the specified value is to be associated
532 >     * @param value value to be associated with the specified key
533       *
534 <     * @return the previous value associated with specified key, or <tt>null</tt>
535 <     *         if there was no mapping for key.  A <tt>null</tt> return can
536 <     *         also indicate that the map previously associated <tt>null</tt>
537 <     *         with the specified key.
538 <     * @throws    ClassCastException if key cannot be compared with the keys
539 <     *            currently in the map.
540 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
541 <     *         natural order, or its comparator does not tolerate
542 <     *         <tt>null</tt> keys.
534 >     * @return the previous value associated with <tt>key</tt>, or
535 >     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
536 >     *         (A <tt>null</tt> return can also indicate that the map
537 >     *         previously associated <tt>null</tt> with <tt>key</tt>.)
538 >     * @throws ClassCastException if the specified key cannot be compared
539 >     *         with the keys currently in the map
540 >     * @throws NullPointerException if the specified key is null
541 >     *         and this map uses natural ordering, or its comparator
542 >     *         does not permit null keys
543       */
544      public V put(K key, V value) {
545          Entry<K,V> t = root;
546  
547          if (t == null) {
548 +            // TBD
549 + //             if (key == null) {
550 + //                 if (comparator == null)
551 + //                     throw new NullPointerException();
552 + //                 comparator.compare(key, key);
553 + //             }
554              incrementSize();
555              root = new Entry<K,V>(key, value, null);
556              return null;
# Line 591 | Line 586 | public class TreeMap<K,V>
586       * Removes the mapping for this key from this TreeMap if present.
587       *
588       * @param  key key for which mapping should be removed
589 <     * @return the previous value associated with specified key, or <tt>null</tt>
590 <     *         if there was no mapping for key.  A <tt>null</tt> return can
591 <     *         also indicate that the map previously associated
592 <     *         <tt>null</tt> with the specified key.
593 <     *
594 <     * @throws    ClassCastException if key cannot be compared with the keys
595 <     *            currently in the map.
596 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
597 <     *         natural order, or its comparator does not tolerate
603 <     *         <tt>null</tt> keys.
589 >     * @return the previous value associated with <tt>key</tt>, or
590 >     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
591 >     *         (A <tt>null</tt> return can also indicate that the map
592 >     *         previously associated <tt>null</tt> with <tt>key</tt>.)
593 >     * @throws ClassCastException if the specified key cannot be compared
594 >     *         with the keys currently in the map
595 >     * @throws NullPointerException if the specified key is null
596 >     *         and this map uses natural ordering, or its comparator
597 >     *         does not permit null keys
598       */
599      public V remove(Object key) {
600          Entry<K,V> p = getEntry(key);
# Line 613 | Line 607 | public class TreeMap<K,V>
607      }
608  
609      /**
610 <     * Removes all mappings from this TreeMap.
610 >     * Removes all of the mappings from this map.
611 >     * The map will be empty after this call returns.
612       */
613      public void clear() {
614          modCount++;
# Line 625 | Line 620 | public class TreeMap<K,V>
620       * Returns a shallow copy of this <tt>TreeMap</tt> instance. (The keys and
621       * values themselves are not cloned.)
622       *
623 <     * @return a shallow copy of this Map.
623 >     * @return a shallow copy of this map
624       */
625      public Object clone() {
626          TreeMap<K,V> clone = null;
# Line 656 | Line 651 | public class TreeMap<K,V>
651      // NavigableMap API methods
652  
653      /**
654 <     * Returns a key-value mapping associated with the least
660 <     * key in this map, or <tt>null</tt> if the map is empty.
661 <     *
662 <     * @return an Entry with least key, or <tt>null</tt>
663 <     * if the map is empty.
654 >     * @since 1.6
655       */
656      public Map.Entry<K,V> firstEntry() {
657          Entry<K,V> e = getFirstEntry();
658 <        return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e);
658 >        return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
659      }
660  
661      /**
662 <     * Returns a key-value mapping associated with the greatest
672 <     * key in this map, or <tt>null</tt> if the map is empty.
673 <     *
674 <     * @return an Entry with greatest key, or <tt>null</tt>
675 <     * if the map is empty.
662 >     * @since 1.6
663       */
664      public Map.Entry<K,V> lastEntry() {
665          Entry<K,V> e = getLastEntry();
666 <        return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e);
666 >        return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
667      }
668  
669      /**
670 <     * Removes and returns a key-value mapping associated with
684 <     * the least key in this map, or <tt>null</tt> if the map is empty.
685 <     *
686 <     * @return the removed first entry of this map, or <tt>null</tt>
687 <     * if the map is empty.
670 >     * @since 1.6
671       */
672      public Map.Entry<K,V> pollFirstEntry() {
673          Entry<K,V> p = getFirstEntry();
674          if (p == null)
675              return null;
676 <        Map.Entry result = new AbstractMap.SimpleImmutableEntry(p);
676 >        Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(p);
677          deleteEntry(p);
678          return result;
679      }
680  
681      /**
682 <     * Removes and returns a key-value mapping associated with
700 <     * the greatest key in this map, or <tt>null</tt> if the map is empty.
701 <     *
702 <     * @return the removed last entry of this map, or <tt>null</tt>
703 <     * if the map is empty.
682 >     * @since 1.6
683       */
684      public Map.Entry<K,V> pollLastEntry() {
685          Entry<K,V> p = getLastEntry();
686          if (p == null)
687              return null;
688 <        Map.Entry result = new AbstractMap.SimpleImmutableEntry(p);
688 >        Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(p);
689          deleteEntry(p);
690          return result;
691      }
692  
693      /**
694 <     * Returns a key-value mapping associated with the least key
695 <     * greater than or equal to the given key, or <tt>null</tt> if
696 <     * there is no such entry.
697 <     *
698 <     * @param key the key.
720 <     * @return an Entry associated with ceiling of given key, or
721 <     * <tt>null</tt> if there is no such Entry.
722 <     * @throws ClassCastException if key cannot be compared with the
723 <     * keys currently in the map.
724 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
725 <     *         natural order, or its comparator does not tolerate
726 <     *         <tt>null</tt> keys.
694 >     * @throws ClassCastException {@inheritDoc}
695 >     * @throws NullPointerException if the specified key is null
696 >     *         and this map uses natural ordering, or its comparator
697 >     *         does not permit null keys
698 >     * @since 1.6
699       */
700 <    public Map.Entry<K,V> ceilingEntry(K key) {
701 <        Entry<K,V> e = getCeilingEntry(key);
702 <        return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e);
700 >    public Map.Entry<K,V> lowerEntry(K key) {
701 >        Entry<K,V> e =  getLowerEntry(key);
702 >        return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
703      }
704  
733
705      /**
706 <     * Returns least key greater than or equal to the given key, or
707 <     * <tt>null</tt> if there is no such key.
708 <     *
709 <     * @param key the key.
710 <     * @return the ceiling key, or <tt>null</tt>
740 <     * if there is no such key.
741 <     * @throws ClassCastException if key cannot be compared with the keys
742 <     *            currently in the map.
743 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
744 <     *         natural order, or its comparator does not tolerate
745 <     *         <tt>null</tt> keys.
706 >     * @throws ClassCastException {@inheritDoc}
707 >     * @throws NullPointerException if the specified key is null
708 >     *         and this map uses natural ordering, or its comparator
709 >     *         does not permit null keys
710 >     * @since 1.6
711       */
712 <    public K ceilingKey(K key) {
713 <        Entry<K,V> e = getCeilingEntry(key);
712 >    public K lowerKey(K key) {
713 >        Entry<K,V> e =  getLowerEntry(key);
714          return (e == null)? null : e.key;
715      }
716  
752
753
717      /**
718 <     * Returns a key-value mapping associated with the greatest key
719 <     * less than or equal to the given key, or <tt>null</tt> if there
720 <     * is no such entry.
721 <     *
722 <     * @param key the key.
760 <     * @return an Entry associated with floor of given key, or <tt>null</tt>
761 <     * if there is no such Entry.
762 <     * @throws ClassCastException if key cannot be compared with the keys
763 <     *            currently in the map.
764 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
765 <     *         natural order, or its comparator does not tolerate
766 <     *         <tt>null</tt> keys.
718 >     * @throws ClassCastException {@inheritDoc}
719 >     * @throws NullPointerException if the specified key is null
720 >     *         and this map uses natural ordering, or its comparator
721 >     *         does not permit null keys
722 >     * @since 1.6
723       */
724      public Map.Entry<K,V> floorEntry(K key) {
725          Entry<K,V> e = getFloorEntry(key);
726 <        return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e);
726 >        return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
727      }
728  
729      /**
730 <     * Returns the greatest key
731 <     * less than or equal to the given key, or <tt>null</tt> if there
732 <     * is no such key.
733 <     *
734 <     * @param key the key.
779 <     * @return the floor of given key, or <tt>null</tt> if there is no
780 <     * such key.
781 <     * @throws ClassCastException if key cannot be compared with the keys
782 <     *            currently in the map.
783 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
784 <     *         natural order, or its comparator does not tolerate
785 <     *         <tt>null</tt> keys.
730 >     * @throws ClassCastException {@inheritDoc}
731 >     * @throws NullPointerException if the specified key is null
732 >     *         and this map uses natural ordering, or its comparator
733 >     *         does not permit null keys
734 >     * @since 1.6
735       */
736      public K floorKey(K key) {
737          Entry<K,V> e = getFloorEntry(key);
# Line 790 | Line 739 | public class TreeMap<K,V>
739      }
740  
741      /**
742 <     * Returns a key-value mapping associated with the least key
743 <     * strictly greater than the given key, or <tt>null</tt> if there
744 <     * is no such entry.
745 <     *
746 <     * @param key the key.
798 <     * @return an Entry with least key greater than the given key, or
799 <     * <tt>null</tt> if there is no such Entry.
800 <     * @throws ClassCastException if key cannot be compared with the keys
801 <     *            currently in the map.
802 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
803 <     *         natural order, or its comparator does not tolerate
804 <     *         <tt>null</tt> keys.
742 >     * @throws ClassCastException {@inheritDoc}
743 >     * @throws NullPointerException if the specified key is null
744 >     *         and this map uses natural ordering, or its comparator
745 >     *         does not permit null keys
746 >     * @since 1.6
747       */
748 <    public Map.Entry<K,V> higherEntry(K key) {
749 <        Entry<K,V> e = getHigherEntry(key);
750 <        return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e);
748 >    public Map.Entry<K,V> ceilingEntry(K key) {
749 >        Entry<K,V> e = getCeilingEntry(key);
750 >        return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
751      }
752  
753      /**
754 <     * Returns the least key strictly greater than the given key, or
755 <     * <tt>null</tt> if there is no such key.
756 <     *
757 <     * @param key the key.
758 <     * @return the least key greater than the given key, or
817 <     * <tt>null</tt> if there is no such key.
818 <     * @throws ClassCastException if key cannot be compared with the keys
819 <     *            currently in the map.
820 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
821 <     *         natural order, or its comparator does not tolerate
822 <     *         <tt>null</tt> keys.
754 >     * @throws ClassCastException {@inheritDoc}
755 >     * @throws NullPointerException if the specified key is null
756 >     *         and this map uses natural ordering, or its comparator
757 >     *         does not permit null keys
758 >     * @since 1.6
759       */
760 <    public K higherKey(K key) {
761 <        Entry<K,V> e = getHigherEntry(key);
760 >    public K ceilingKey(K key) {
761 >        Entry<K,V> e = getCeilingEntry(key);
762          return (e == null)? null : e.key;
763      }
764  
765      /**
766 <     * Returns a key-value mapping associated with the greatest
767 <     * key strictly less than the given key, or <tt>null</tt> if there is no
768 <     * such entry.
769 <     *
770 <     * @param key the key.
835 <     * @return an Entry with greatest key less than the given
836 <     * key, or <tt>null</tt> if there is no such Entry.
837 <     * @throws ClassCastException if key cannot be compared with the keys
838 <     *            currently in the map.
839 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
840 <     *         natural order, or its comparator does not tolerate
841 <     *         <tt>null</tt> keys.
766 >     * @throws ClassCastException {@inheritDoc}
767 >     * @throws NullPointerException if the specified key is null
768 >     *         and this map uses natural ordering, or its comparator
769 >     *         does not permit null keys
770 >     * @since 1.6
771       */
772 <    public Map.Entry<K,V> lowerEntry(K key) {
773 <        Entry<K,V> e =  getLowerEntry(key);
774 <        return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e);
772 >    public Map.Entry<K,V> higherEntry(K key) {
773 >        Entry<K,V> e = getHigherEntry(key);
774 >        return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
775      }
776  
777      /**
778 <     * Returns the greatest key strictly less than the given key, or
779 <     * <tt>null</tt> if there is no such key.
780 <     *
781 <     * @param key the key.
782 <     * @return the greatest key less than the given
854 <     * key, or <tt>null</tt> if there is no such key.
855 <     * @throws ClassCastException if key cannot be compared with the keys
856 <     *            currently in the map.
857 <     * @throws NullPointerException if key is <tt>null</tt> and this map uses
858 <     *         natural order, or its comparator does not tolerate
859 <     *         <tt>null</tt> keys.
778 >     * @throws ClassCastException {@inheritDoc}
779 >     * @throws NullPointerException if the specified key is null
780 >     *         and this map uses natural ordering, or its comparator
781 >     *         does not permit null keys
782 >     * @since 1.6
783       */
784 <    public K lowerKey(K key) {
785 <        Entry<K,V> e =  getLowerEntry(key);
784 >    public K higherKey(K key) {
785 >        Entry<K,V> e = getHigherEntry(key);
786          return (e == null)? null : e.key;
787      }
788  
# Line 875 | Line 798 | public class TreeMap<K,V>
798      private transient Set<K> descendingKeySet = null;
799  
800      /**
801 <     * Returns a Set view of the keys contained in this map.  The set's
802 <     * iterator will return the keys in ascending order.  The set is backed by
803 <     * this <tt>TreeMap</tt> instance, so changes to this map are reflected in
804 <     * the Set, and vice-versa.  The Set supports element removal, which
805 <     * removes the corresponding mapping from the map, via the
806 <     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
807 <     * <tt>retainAll</tt>, and <tt>clear</tt> operations.  It does not support
808 <     * the <tt>add</tt> or <tt>addAll</tt> operations.
809 <     *
810 <     * @return a set view of the keys contained in this TreeMap.
801 >     * Returns a {@link Set} view of the keys contained in this map.
802 >     * The set's iterator returns the keys in ascending order.
803 >     * The set is backed by the map, so changes to the map are
804 >     * reflected in the set, and vice-versa.  If the map is modified
805 >     * while an iteration over the set is in progress (except through
806 >     * the iterator's own <tt>remove</tt> operation), the results of
807 >     * the iteration are undefined.  The set supports element removal,
808 >     * which removes the corresponding mapping from the map, via the
809 >     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
810 >     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
811 >     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
812 >     * operations.
813       */
814      public Set<K> keySet() {
815          Set<K> ks = keySet;
# Line 916 | Line 841 | public class TreeMap<K,V>
841      }
842  
843      /**
844 <     * Returns a collection view of the values contained in this map.  The
845 <     * collection's iterator will return the values in the order that their
846 <     * corresponding keys appear in the tree.  The collection is backed by
847 <     * this <tt>TreeMap</tt> instance, so changes to this map are reflected in
848 <     * the collection, and vice-versa.  The collection supports element
849 <     * removal, which removes the corresponding mapping from the map through
850 <     * the <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
851 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
852 <     * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
853 <     *
854 <     * @return a collection view of the values contained in this map.
844 >     * Returns a {@link Collection} view of the values contained in this map.
845 >     * The collection's iterator returns the values in ascending order
846 >     * of the corresponding keys.
847 >     * The collection is backed by the map, so changes to the map are
848 >     * reflected in the collection, and vice-versa.  If the map is
849 >     * modified while an iteration over the collection is in progress
850 >     * (except through the iterator's own <tt>remove</tt> operation),
851 >     * the results of the iteration are undefined.  The collection
852 >     * supports element removal, which removes the corresponding
853 >     * mapping from the map, via the <tt>Iterator.remove</tt>,
854 >     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
855 >     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
856 >     * support the <tt>add</tt> or <tt>addAll</tt> operations.
857       */
858      public Collection<V> values() {
859          Collection<V> vs = values;
# Line 965 | Line 892 | public class TreeMap<K,V>
892      }
893  
894      /**
895 <     * Returns a set view of the mappings contained in this map.  The set's
896 <     * iterator returns the mappings in ascending key order.  Each element in
897 <     * the returned set is a <tt>Map.Entry</tt>.  The set is backed by this
898 <     * map, so changes to this map are reflected in the set, and vice-versa.
899 <     * The set supports element removal, which removes the corresponding
900 <     * mapping from the TreeMap, through the <tt>Iterator.remove</tt>,
895 >     * Returns a {@link Set} view of the mappings contained in this map.
896 >     * The set's iterator returns the entries in ascending key order.
897 >     * The set is backed by the map, so changes to the map are
898 >     * reflected in the set, and vice-versa.  If the map is modified
899 >     * while an iteration over the set is in progress (except through
900 >     * the iterator's own <tt>remove</tt> operation, or through the
901 >     * <tt>setValue</tt> operation on a map entry returned by the
902 >     * iterator) the results of the iteration are undefined.  The set
903 >     * supports element removal, which removes the corresponding
904 >     * mapping from the map, via the <tt>Iterator.remove</tt>,
905       * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
906 <     * <tt>clear</tt> operations.  It does not support the <tt>add</tt> or
907 <     * <tt>addAll</tt> operations.
977 <     *
978 <     * @return a set view of the mappings contained in this map.
979 <     * @see Map.Entry
906 >     * <tt>clear</tt> operations.  It does not support the
907 >     * <tt>add</tt> or <tt>addAll</tt> operations.
908       */
909      public Set<Map.Entry<K,V>> entrySet() {
910          Set<Map.Entry<K,V>> es = entrySet;
# Line 1020 | Line 948 | public class TreeMap<K,V>
948      }
949  
950      /**
951 <     * Returns a set view of the mappings contained in this map.  The
1024 <     * set's iterator returns the mappings in descending key order.
1025 <     * Each element in the returned set is a <tt>Map.Entry</tt>.  The
1026 <     * set is backed by this map, so changes to this map are reflected
1027 <     * in the set, and vice-versa.  The set supports element removal,
1028 <     * which removes the corresponding mapping from the TreeMap,
1029 <     * through the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
1030 <     * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt>
1031 <     * operations.  It does not support the <tt>add</tt> or
1032 <     * <tt>addAll</tt> operations.
1033 <     *
1034 <     * @return a set view of the mappings contained in this map, in
1035 <     * descending key order
1036 <     * @see Map.Entry
951 >     * @since 1.6
952       */
953      public Set<Map.Entry<K,V>> descendingEntrySet() {
954          Set<Map.Entry<K,V>> es = descendingEntrySet;
# Line 1047 | Line 962 | public class TreeMap<K,V>
962      }
963  
964      /**
965 <     * Returns a Set view of the keys contained in this map.  The
1051 <     * set's iterator will return the keys in descending order.  The
1052 <     * map is backed by this <tt>TreeMap</tt> instance, so changes to
1053 <     * this map are reflected in the Set, and vice-versa.  The Set
1054 <     * supports element removal, which removes the corresponding
1055 <     * mapping from the map, via the <tt>Iterator.remove</tt>,
1056 <     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>,
1057 <     * and <tt>clear</tt> operations.  It does not support the
1058 <     * <tt>add</tt> or <tt>addAll</tt> operations.
1059 <     *
1060 <     * @return a set view of the keys contained in this TreeMap.
965 >     * @since 1.6
966       */
967      public Set<K> descendingKeySet() {
968          Set<K> ks = descendingKeySet;
# Line 1071 | Line 976 | public class TreeMap<K,V>
976      }
977  
978      /**
979 <     * Returns a view of the portion of this map whose keys range from
1075 <     * <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.  (If
1076 <     * <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned
1077 <     * navigable map is empty.)  The returned navigable map is backed
1078 <     * by this map, so changes in the returned navigable map are
1079 <     * reflected in this map, and vice-versa.  The returned navigable
1080 <     * map supports all optional map operations.<p>
1081 <     *
1082 <     * The navigable map returned by this method will throw an
1083 <     * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
1084 <     * less than <tt>fromKey</tt> or greater than or equal to
1085 <     * <tt>toKey</tt>.<p>
1086 <     *
1087 <     * Note: this method always returns a <i>half-open range</i> (which
1088 <     * includes its low endpoint but not its high endpoint).  If you need a
1089 <     * <i>closed range</i> (which includes both endpoints), and the key type
1090 <     * allows for calculation of the successor of a given key, merely request the
1091 <     * subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>.
1092 <     * For example, suppose that <tt>m</tt> is a navigable map whose keys are
1093 <     * strings.  The following idiom obtains a view containing all of the
1094 <     * key-value mappings in <tt>m</tt> whose keys are between <tt>low</tt>
1095 <     * and <tt>high</tt>, inclusive:
1096 <     * <pre>  NavigableMap sub = m.navigableSubMap(low, high+"\0");</pre>
1097 <     * A similar technique can be used to generate an <i>open range</i> (which
1098 <     * contains neither endpoint).  The following idiom obtains a view
1099 <     * containing all of the key-value mappings in <tt>m</tt> whose keys are
1100 <     * between <tt>low</tt> and <tt>high</tt>, exclusive:
1101 <     * <pre>  NavigableMap sub = m.navigableSubMap(low+"\0", high);</pre>
1102 <     *
1103 <     * @param fromKey low endpoint (inclusive) of the subMap.
1104 <     * @param toKey high endpoint (exclusive) of the subMap.
1105 <     *
1106 <     * @return a view of the portion of this map whose keys range from
1107 <     *                <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.
1108 <     *
1109 <     * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
1110 <     *         cannot be compared to one another using this map's comparator
1111 <     *         (or, if the map has no comparator, using natural ordering).
1112 <     * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
1113 <     *         <tt>toKey</tt>.
979 >     * @throws ClassCastException       {@inheritDoc}
980       * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
981 <     *               <tt>null</tt> and this map uses natural order, or its
982 <     *               comparator does not tolerate <tt>null</tt> keys.
981 >     *         null and this map uses natural ordering, or its comparator
982 >     *         does not permit null keys
983 >     * @throws IllegalArgumentException {@inheritDoc}
984 >     * @since 1.6
985       */
986      public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) {
987          return new SubMap(fromKey, toKey);
988      }
989  
1122
990      /**
991 <     * Returns a view of the portion of this map whose keys are strictly less
992 <     * than <tt>toKey</tt>.  The returned navigable map is backed by this map, so
993 <     * changes in the returned navigable map are reflected in this map, and
994 <     * vice-versa.  The returned navigable map supports all optional map
995 <     * operations.<p>
996 <     *
1130 <     * The navigable map returned by this method will throw an
1131 <     * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
1132 <     * greater than or equal to <tt>toKey</tt>.<p>
1133 <     *
1134 <     * Note: this method always returns a view that does not contain its
1135 <     * (high) endpoint.  If you need a view that does contain this endpoint,
1136 <     * and the key type allows for calculation of the successor of a given key,
1137 <     * merely request a headMap bounded by <tt>successor(highEndpoint)</tt>.
1138 <     * For example, suppose that suppose that <tt>m</tt> is a navigable map whose
1139 <     * keys are strings.  The following idiom obtains a view containing all of
1140 <     * the key-value mappings in <tt>m</tt> whose keys are less than or equal
1141 <     * to <tt>high</tt>:
1142 <     * <pre>
1143 <     *     NavigableMap head = m.navigableHeadMap(high+"\0");
1144 <     * </pre>
1145 <     *
1146 <     * @param toKey high endpoint (exclusive) of the headMap.
1147 <     * @return a view of the portion of this map whose keys are strictly
1148 <     *                less than <tt>toKey</tt>.
1149 <     *
1150 <     * @throws ClassCastException if <tt>toKey</tt> is not compatible
1151 <     *         with this map's comparator (or, if the map has no comparator,
1152 <     *         if <tt>toKey</tt> does not implement <tt>Comparable</tt>).
1153 <     * @throws IllegalArgumentException if this map is itself a subMap,
1154 <     *         headMap, or tailMap, and <tt>toKey</tt> is not within the
1155 <     *         specified range of the subMap, headMap, or tailMap.
1156 <     * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and
1157 <     *               this map uses natural order, or its comparator does not
1158 <     *               tolerate <tt>null</tt> keys.
991 >     * @throws ClassCastException       {@inheritDoc}
992 >     * @throws NullPointerException if <tt>toKey</tt> is null
993 >     *         and this map uses natural ordering, or its comparator
994 >     *         does not permit null keys
995 >     * @throws IllegalArgumentException {@inheritDoc}
996 >     * @since 1.6
997       */
998      public NavigableMap<K,V> navigableHeadMap(K toKey) {
999          return new SubMap(toKey, true);
1000      }
1001  
1002      /**
1003 <     * Returns a view of the portion of this map whose keys are greater than
1004 <     * or equal to <tt>fromKey</tt>.  The returned navigable map is backed by
1005 <     * this map, so changes in the returned navigable map are reflected in this
1006 <     * map, and vice-versa.  The returned navigable map supports all optional map
1007 <     * operations.<p>
1008 <     *
1171 <     * The navigable map returned by this method will throw an
1172 <     * <tt>IllegalArgumentException</tt> if the user attempts to insert a key
1173 <     * less than <tt>fromKey</tt>.<p>
1174 <     *
1175 <     * Note: this method always returns a view that contains its (low)
1176 <     * endpoint.  If you need a view that does not contain this endpoint, and
1177 <     * the element type allows for calculation of the successor of a given value,
1178 <     * merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>.
1179 <     * For example, suppose that <tt>m</tt> is a navigable map whose keys
1180 <     * are strings.  The following idiom obtains a view containing
1181 <     * all of the key-value mappings in <tt>m</tt> whose keys are strictly
1182 <     * greater than <tt>low</tt>: <pre>
1183 <     *     NavigableMap tail = m.navigableTailMap(low+"\0");
1184 <     * </pre>
1185 <     *
1186 <     * @param fromKey low endpoint (inclusive) of the tailMap.
1187 <     * @return a view of the portion of this map whose keys are greater
1188 <     *                than or equal to <tt>fromKey</tt>.
1189 <     * @throws ClassCastException if <tt>fromKey</tt> is not compatible
1190 <     *         with this map's comparator (or, if the map has no comparator,
1191 <     *         if <tt>fromKey</tt> does not implement <tt>Comparable</tt>).
1192 <     * @throws IllegalArgumentException if this map is itself a subMap,
1193 <     *         headMap, or tailMap, and <tt>fromKey</tt> is not within the
1194 <     *         specified range of the subMap, headMap, or tailMap.
1195 <     * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and
1196 <     *               this map uses natural order, or its comparator does not
1197 <     *               tolerate <tt>null</tt> keys.
1003 >     * @throws ClassCastException       {@inheritDoc}
1004 >     * @throws NullPointerException if <tt>fromKey</tt> is null
1005 >     *         and this map uses natural ordering, or its comparator
1006 >     *         does not permit null keys
1007 >     * @throws IllegalArgumentException {@inheritDoc}
1008 >     * @since 1.6
1009       */
1010      public NavigableMap<K,V> navigableTailMap(K fromKey) {
1011          return new SubMap(fromKey, false);
1012      }
1013  
1014      /**
1015 <     * Equivalent to <tt>navigableSubMap</tt> but with a return
1016 <     * type conforming to the <tt>SortedMap</tt> interface.
1017 <     * @param fromKey low endpoint (inclusive) of the subMap.
1018 <     * @param toKey high endpoint (exclusive) of the subMap.
1019 <     *
1020 <     * @return a view of the portion of this map whose keys range from
1210 <     *                <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive.
1211 <     *
1212 <     * @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt>
1213 <     *         cannot be compared to one another using this map's comparator
1214 <     *         (or, if the map has no comparator, using natural ordering).
1215 <     * @throws IllegalArgumentException if <tt>fromKey</tt> is greater than
1216 <     *         <tt>toKey</tt>.
1015 >     * Equivalent to {@link #navigableSubMap} but with a return type
1016 >     * conforming to the <tt>SortedMap</tt> interface.
1017 >     *
1018 >     * <p>{@inheritDoc}
1019 >     *
1020 >     * @throws ClassCastException       {@inheritDoc}
1021       * @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is
1022 <     *               <tt>null</tt> and this map uses natural order, or its
1023 <     *               comparator does not tolerate <tt>null</tt> keys.
1022 >     *         null and this map uses natural ordering, or its comparator
1023 >     *         does not permit null keys
1024 >     * @throws IllegalArgumentException {@inheritDoc}
1025       */
1026      public SortedMap<K,V> subMap(K fromKey, K toKey) {
1027          return new SubMap(fromKey, toKey);
1028      }
1029  
1225
1030      /**
1031 <     * Equivalent to <tt>navigableHeadMap</tt> but with a return
1032 <     * type conforming to the <tt>SortedMap</tt> interface.
1031 >     * Equivalent to {@link #navigableHeadMap} but with a return type
1032 >     * conforming to the <tt>SortedMap</tt> interface.
1033       *
1034 <     * @param toKey high endpoint (exclusive) of the headMap.
1035 <     * @return a view of the portion of this map whose keys are strictly
1036 <     *                less than <tt>toKey</tt>.
1037 <     *
1038 <     * @throws ClassCastException if <tt>toKey</tt> is not compatible
1039 <     *         with this map's comparator (or, if the map has no comparator,
1040 <     *         if <tt>toKey</tt> does not implement <tt>Comparable</tt>).
1237 <     * @throws IllegalArgumentException if this map is itself a subMap,
1238 <     *         headMap, or tailMap, and <tt>toKey</tt> is not within the
1239 <     *         specified range of the subMap, headMap, or tailMap.
1240 <     * @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and
1241 <     *               this map uses natural order, or its comparator does not
1242 <     *               tolerate <tt>null</tt> keys.
1034 >     * <p>{@inheritDoc}
1035 >     *
1036 >     * @throws ClassCastException       {@inheritDoc}
1037 >     * @throws NullPointerException if <tt>toKey</tt> is null
1038 >     *         and this map uses natural ordering, or its comparator
1039 >     *         does not permit null keys
1040 >     * @throws IllegalArgumentException {@inheritDoc}
1041       */
1042      public SortedMap<K,V> headMap(K toKey) {
1043          return new SubMap(toKey, true);
1044      }
1045  
1046      /**
1047 <     * Equivalent to <tt>navigableTailMap</tt> but with a return
1048 <     * type conforming to the <tt>SortedMap</tt> interface.
1047 >     * Equivalent to {@link #navigableTailMap} but with a return type
1048 >     * conforming to the <tt>SortedMap</tt> interface.
1049 >     *
1050 >     * <p>{@inheritDoc}
1051       *
1052 <     * @param fromKey low endpoint (inclusive) of the tailMap.
1053 <     * @return a view of the portion of this map whose keys are greater
1054 <     *                than or equal to <tt>fromKey</tt>.
1055 <     * @throws ClassCastException if <tt>fromKey</tt> is not compatible
1056 <     *         with this map's comparator (or, if the map has no comparator,
1257 <     *         if <tt>fromKey</tt> does not implement <tt>Comparable</tt>).
1258 <     * @throws IllegalArgumentException if this map is itself a subMap,
1259 <     *         headMap, or tailMap, and <tt>fromKey</tt> is not within the
1260 <     *         specified range of the subMap, headMap, or tailMap.
1261 <     * @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and
1262 <     *               this map uses natural order, or its comparator does not
1263 <     *               tolerate <tt>null</tt> keys.
1052 >     * @throws ClassCastException       {@inheritDoc}
1053 >     * @throws NullPointerException if <tt>fromKey</tt> is null
1054 >     *         and this map uses natural ordering, or its comparator
1055 >     *         does not permit null keys
1056 >     * @throws IllegalArgumentException {@inheritDoc}
1057       */
1058      public SortedMap<K,V> tailMap(K fromKey) {
1059          return new SubMap(fromKey, false);
# Line 1309 | Line 1102 | public class TreeMap<K,V>
1102          }
1103  
1104          public boolean containsKey(Object key) {
1105 <            return inRange((K) key) && TreeMap.this.containsKey(key);
1105 >            return inRange(key) && TreeMap.this.containsKey(key);
1106          }
1107  
1108          public V get(Object key) {
1109 <            if (!inRange((K) key))
1109 >            if (!inRange(key))
1110                  return null;
1111              return TreeMap.this.get(key);
1112          }
# Line 1325 | Line 1118 | public class TreeMap<K,V>
1118          }
1119  
1120          public V remove(Object key) {
1121 <            if (!inRange((K) key))
1121 >            if (!inRange(key))
1122                  return null;
1123              return TreeMap.this.remove(key);
1124          }
# Line 1371 | Line 1164 | public class TreeMap<K,V>
1164                  getFirstEntry() : getCeilingEntry(fromKey);
1165              if (e == null || (!toEnd && compare(e.key, toKey) >= 0))
1166                  return null;
1167 <            Map.Entry result = new AbstractMap.SimpleImmutableEntry(e);
1167 >            Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(e);
1168              deleteEntry(e);
1169              return result;
1170          }
# Line 1381 | Line 1174 | public class TreeMap<K,V>
1174                  getLastEntry() : getLowerEntry(toKey);
1175              if (e == null || (!fromStart && compare(e.key, fromKey) < 0))
1176                  return null;
1177 <            Map.Entry result = new AbstractMap.SimpleImmutableEntry(e);
1177 >            Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(e);
1178              deleteEntry(e);
1179              return result;
1180          }
# Line 1396 | Line 1189 | public class TreeMap<K,V>
1189  
1190          public Map.Entry<K,V> ceilingEntry(K key) {
1191              TreeMap.Entry<K,V> e = subceiling(key);
1192 <            return e == null? null : new AbstractMap.SimpleImmutableEntry(e);
1192 >            return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
1193          }
1194  
1195          public K ceilingKey(K key) {
# Line 1415 | Line 1208 | public class TreeMap<K,V>
1208  
1209          public Map.Entry<K,V> higherEntry(K key) {
1210              TreeMap.Entry<K,V> e = subhigher(key);
1211 <            return e == null? null : new AbstractMap.SimpleImmutableEntry(e);
1211 >            return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
1212          }
1213  
1214          public K higherKey(K key) {
# Line 1433 | Line 1226 | public class TreeMap<K,V>
1226  
1227          public Map.Entry<K,V> floorEntry(K key) {
1228              TreeMap.Entry<K,V> e = subfloor(key);
1229 <            return e == null? null : new AbstractMap.SimpleImmutableEntry(e);
1229 >            return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
1230          }
1231  
1232          public K floorKey(K key) {
# Line 1451 | Line 1244 | public class TreeMap<K,V>
1244  
1245          public Map.Entry<K,V> lowerEntry(K key) {
1246              TreeMap.Entry<K,V> e = sublower(key);
1247 <            return e == null? null : new AbstractMap.SimpleImmutableEntry(e);
1247 >            return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e);
1248          }
1249  
1250          public K lowerKey(K key) {
# Line 1524 | Line 1317 | public class TreeMap<K,V>
1317  
1318          public Set<Map.Entry<K,V>> descendingEntrySet() {
1319              Set<Map.Entry<K,V>> es = descendingEntrySetView;
1320 <            return (es != null) ? es : (descendingEntrySetView = new DescendingEntrySetView());
1320 >            return (es != null) ? es :
1321 >                (descendingEntrySetView = new DescendingEntrySetView());
1322          }
1323  
1324          public Set<K> descendingKeySet() {
1325              Set<K> ks = descendingKeySetView;
1326 <            return (ks != null) ? ks : (descendingKeySetView = new DescendingKeySetView());
1326 >            return (ks != null) ? ks :
1327 >                (descendingKeySetView = new DescendingKeySetView());
1328          }
1329  
1330          private class DescendingEntrySetView extends EntrySetView {
# Line 1560 | Line 1355 | public class TreeMap<K,V>
1355              }
1356          }
1357  
1563
1358          public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) {
1359              if (!inRange2(fromKey))
1360                  throw new IllegalArgumentException("fromKey out of range");
# Line 1581 | Line 1375 | public class TreeMap<K,V>
1375              return new SubMap(false, fromKey, toEnd, toKey);
1376          }
1377  
1584
1378          public SortedMap<K,V> subMap(K fromKey, K toKey) {
1379              return navigableSubMap(fromKey, toKey);
1380          }
# Line 1594 | Line 1387 | public class TreeMap<K,V>
1387              return navigableTailMap(fromKey);
1388          }
1389  
1390 <        private boolean inRange(K key) {
1390 >        private boolean inRange(Object key) {
1391              return (fromStart || compare(key, fromKey) >= 0) &&
1392                     (toEnd     || compare(key, toKey)   <  0);
1393          }
1394  
1395          // This form allows the high endpoint (as well as all legit keys)
1396 <        private boolean inRange2(K key) {
1396 >        private boolean inRange2(Object key) {
1397              return (fromStart || compare(key, fromKey) >= 0) &&
1398                     (toEnd     || compare(key, toKey)   <= 0);
1399          }
# Line 1622 | Line 1415 | public class TreeMap<K,V>
1415              return next != null;
1416          }
1417  
1418 <        Entry<K,V> nextEntry() {
1418 >        Entry<K,V> nextEntry() {
1419              if (next == null)
1420                  throw new NoSuchElementException();
1421              if (modCount != expectedModCount)
# Line 1649 | Line 1442 | public class TreeMap<K,V>
1442          EntryIterator(Entry<K,V> first) {
1443              super(first);
1444          }
1652
1445          public Map.Entry<K,V> next() {
1446              return nextEntry();
1447          }
# Line 1694 | Line 1486 | public class TreeMap<K,V>
1486          }
1487      }
1488  
1697
1489      /**
1490       * Base for Descending Iterators.
1491       */
# Line 1755 | Line 1546 | public class TreeMap<K,V>
1546  
1547      }
1548  
1758
1549      /**
1550       * Compares two keys using the correct comparison method for this TreeMap.
1551       */
1552 <    private int compare(K k1, K k2) {
1553 <        return comparator==null ? ((Comparable<? super K>)k1).compareTo(k2)
1554 <                                : comparator.compare(k1, k2);
1552 >    private int compare(Object k1, Object k2) {
1553 >        return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
1554 >                                : comparator.compare((K)k1, (K)k2);
1555      }
1556  
1557      /**
1558 <     * Test two values  for equality.  Differs from o1.equals(o2) only in
1558 >     * Test two values for equality.  Differs from o1.equals(o2) only in
1559       * that it copes with <tt>null</tt> o1 properly.
1560       */
1561      private static boolean valEquals(Object o1, Object o2) {
# Line 1801 | Line 1591 | public class TreeMap<K,V>
1591          /**
1592           * Returns the key.
1593           *
1594 <         * @return the key.
1594 >         * @return the key
1595           */
1596          public K getKey() {
1597              return key;
# Line 1810 | Line 1600 | public class TreeMap<K,V>
1600          /**
1601           * Returns the value associated with the key.
1602           *
1603 <         * @return the value associated with the key.
1603 >         * @return the value associated with the key
1604           */
1605          public V getValue() {
1606              return value;
# Line 1821 | Line 1611 | public class TreeMap<K,V>
1611           * value.
1612           *
1613           * @return the value associated with the key before this method was
1614 <         *           called.
1614 >         *         called
1615           */
1616          public V setValue(V value) {
1617              V oldValue = this.value;
# Line 2160 | Line 1950 | public class TreeMap<K,V>
1950          // Write out size (number of Mappings)
1951          s.writeInt(size);
1952  
2163        Set<Map.Entry<K,V>> es = entrySet();
1953          // Write out keys and values (alternating)
1954 <        for (Iterator<Map.Entry<K,V>> i = es.iterator(); i.hasNext(); ) {
1954 >        for (Iterator<Map.Entry<K,V>> i = entrySet().iterator(); i.hasNext(); ) {
1955              Map.Entry<K,V> e = i.next();
1956              s.writeObject(e.getKey());
1957              s.writeObject(e.getValue());
# Line 2218 | Line 2007 | public class TreeMap<K,V>
2007       * to calling this method.
2008       *
2009       * @param size the number of keys (or key-value pairs) to be read from
2010 <     *        the iterator or stream.
2010 >     *        the iterator or stream
2011       * @param it If non-null, new entries are created from entries
2012       *        or keys read from this iterator.
2013       * @param str If non-null, new entries are created from keys and
# Line 2253 | Line 2042 | public class TreeMap<K,V>
2042       * @param level the current level of tree. Initial call should be 0.
2043       * @param lo the first element index of this subtree. Initial should be 0.
2044       * @param hi the last element index of this subtree.  Initial should be
2045 <     *              size-1.
2045 >     *        size-1.
2046       * @param redLevel the level at which nodes should be red.
2047       *        Must be equal to computeRedLevel for tree of this size.
2048       */
# Line 2337 | Line 2126 | public class TreeMap<K,V>
2126              level++;
2127          return level;
2128      }
2340
2129   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines