1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
8 |
< |
package java.util; |
9 |
< |
|
8 |
> |
package java.util; |
9 |
> |
import java.util.*; // for javadoc |
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 |
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 |
34 |
> |
* <p><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 |
43 |
|
* time, to prevent accidental unsynchronized access to the map: |
44 |
|
* <pre> |
45 |
|
* Map m = Collections.synchronizedMap(new TreeMap(...)); |
46 |
< |
* </pre><p> |
46 |
> |
* </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 |
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 |
92 |
|
implements NavigableMap<K,V>, Cloneable, java.io.Serializable |
93 |
|
{ |
94 |
|
/** |
95 |
< |
* The Comparator used to maintain order in this TreeMap, or |
96 |
< |
* null if this TreeMap uses its elements natural ordering. |
95 |
> |
* The comparator used to maintain order in this tree map, or |
96 |
> |
* null if it uses the natural ordering of its keys. |
97 |
|
* |
98 |
|
* @serial |
99 |
|
*/ |
115 |
|
private void decrementSize() { modCount++; size--; } |
116 |
|
|
117 |
|
/** |
118 |
< |
* Constructs a new, empty map, sorted according to the keys' natural |
119 |
< |
* order. All keys inserted into the map must implement the |
120 |
< |
* <tt>Comparable</tt> interface. Furthermore, all such keys must be |
121 |
< |
* <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw a |
122 |
< |
* ClassCastException for any elements <tt>k1</tt> and <tt>k2</tt> in the |
123 |
< |
* map. If the user attempts to put a key into the map that violates this |
124 |
< |
* constraint (for example, the user attempts to put a string key into a |
125 |
< |
* map whose keys are integers), the <tt>put(Object key, Object |
126 |
< |
* value)</tt> call will throw a <tt>ClassCastException</tt>. |
127 |
< |
* |
126 |
< |
* @see Comparable |
118 |
> |
* Constructs a new, empty tree map, using the natural ordering of its |
119 |
> |
* keys. All keys inserted into the map must implement the {@link |
120 |
> |
* Comparable} interface. Furthermore, all such keys must be |
121 |
> |
* <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw |
122 |
> |
* a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and |
123 |
> |
* <tt>k2</tt> in the map. If the user attempts to put a key into the |
124 |
> |
* map that violates this constraint (for example, the user attempts to |
125 |
> |
* put a string key into a map whose keys are integers), the |
126 |
> |
* <tt>put(Object key, Object value)</tt> call will throw a |
127 |
> |
* <tt>ClassCastException</tt>. |
128 |
|
*/ |
129 |
|
public TreeMap() { |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* Constructs a new, empty map, sorted according to the given comparator. |
134 |
< |
* All keys inserted into the map must be <i>mutually comparable</i> by |
135 |
< |
* the given comparator: <tt>comparator.compare(k1, k2)</tt> must not |
136 |
< |
* throw a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and |
137 |
< |
* <tt>k2</tt> in the map. If the user attempts to put a key into the |
138 |
< |
* map that violates this constraint, the <tt>put(Object key, Object |
139 |
< |
* value)</tt> call will throw a <tt>ClassCastException</tt>. |
140 |
< |
* |
141 |
< |
* @param c the comparator that will be used to sort this map. A |
142 |
< |
* <tt>null</tt> value indicates that the keys' <i>natural |
143 |
< |
* ordering</i> should be used. |
144 |
< |
*/ |
145 |
< |
public TreeMap(Comparator<? super K> c) { |
146 |
< |
this.comparator = c; |
133 |
> |
* Constructs a new, empty tree map, ordered according to the given |
134 |
> |
* comparator. All keys inserted into the map must be <i>mutually |
135 |
> |
* comparable</i> by the given comparator: <tt>comparator.compare(k1, |
136 |
> |
* k2)</tt> must not throw a <tt>ClassCastException</tt> for any keys |
137 |
> |
* <tt>k1</tt> and <tt>k2</tt> in the map. If the user attempts to put |
138 |
> |
* a key into the map that violates this constraint, the <tt>put(Object |
139 |
> |
* key, Object value)</tt> call will throw a |
140 |
> |
* <tt>ClassCastException</tt>. |
141 |
> |
* |
142 |
> |
* @param comparator the comparator that will be used to order this map. |
143 |
> |
* If <tt>null</tt>, the {@linkplain Comparable natural |
144 |
> |
* ordering} of the keys will be used. |
145 |
> |
*/ |
146 |
> |
public TreeMap(Comparator<? super K> comparator) { |
147 |
> |
this.comparator = comparator; |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* Constructs a new map containing the same mappings as the given map, |
152 |
< |
* sorted according to the keys' <i>natural order</i>. All keys inserted |
153 |
< |
* into the new map must implement the <tt>Comparable</tt> interface. |
154 |
< |
* Furthermore, all such keys must be <i>mutually comparable</i>: |
155 |
< |
* <tt>k1.compareTo(k2)</tt> must not throw a <tt>ClassCastException</tt> |
156 |
< |
* for any elements <tt>k1</tt> and <tt>k2</tt> in the map. This method |
157 |
< |
* runs in n*log(n) time. |
158 |
< |
* |
159 |
< |
* @param m the map whose mappings are to be placed in this map. |
160 |
< |
* @throws ClassCastException the keys in t are not Comparable, or |
161 |
< |
* are not mutually comparable. |
162 |
< |
* @throws NullPointerException if the specified map is null. |
151 |
> |
* Constructs a new tree map containing the same mappings as the given |
152 |
> |
* map, ordered according to the <i>natural ordering</i> of its keys. |
153 |
> |
* All keys inserted into the new map must implement the {@link |
154 |
> |
* Comparable} interface. Furthermore, all such keys must be |
155 |
> |
* <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw |
156 |
> |
* a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and |
157 |
> |
* <tt>k2</tt> in the map. This method runs in n*log(n) time. |
158 |
> |
* |
159 |
> |
* @param m the map whose mappings are to be placed in this map |
160 |
> |
* @throws ClassCastException if the keys in m are not {@link Comparable}, |
161 |
> |
* or are not mutually comparable |
162 |
> |
* @throws NullPointerException if the specified map is null |
163 |
|
*/ |
164 |
|
public TreeMap(Map<? extends K, ? extends V> m) { |
165 |
|
putAll(m); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* Constructs a new map containing the same mappings as the given |
170 |
< |
* <tt>SortedMap</tt>, sorted according to the same ordering. This method |
171 |
< |
* runs in linear time. |
169 |
> |
* Constructs a new tree map containing the same mappings and |
170 |
> |
* using the same ordering as the specified sorted map. This |
171 |
> |
* method runs in linear time. |
172 |
|
* |
173 |
|
* @param m the sorted map whose mappings are to be placed in this map, |
174 |
< |
* and whose comparator is to be used to sort this map. |
175 |
< |
* @throws NullPointerException if the specified sorted map is null. |
174 |
> |
* and whose comparator is to be used to sort this map |
175 |
> |
* @throws NullPointerException if the specified map is null |
176 |
|
*/ |
177 |
|
public TreeMap(SortedMap<K, ? extends V> m) { |
178 |
|
comparator = m.comparator(); |
189 |
|
/** |
190 |
|
* Returns the number of key-value mappings in this map. |
191 |
|
* |
192 |
< |
* @return the number of key-value mappings in this map. |
192 |
> |
* @return the number of key-value mappings in this map |
193 |
|
*/ |
194 |
|
public int size() { |
195 |
|
return size; |
199 |
|
* Returns <tt>true</tt> if this map contains a mapping for the specified |
200 |
|
* key. |
201 |
|
* |
202 |
< |
* @param key key whose presence in this map is to be tested. |
201 |
< |
* |
202 |
> |
* @param key key whose presence in this map is to be tested |
203 |
|
* @return <tt>true</tt> if this map contains a mapping for the |
204 |
< |
* specified key. |
205 |
< |
* @throws ClassCastException if the key cannot be compared with the keys |
206 |
< |
* currently in the map. |
207 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
208 |
< |
* natural ordering, or its comparator does not tolerate |
209 |
< |
* <tt>null</tt> keys. |
204 |
> |
* specified key |
205 |
> |
* @throws ClassCastException if the specified key cannot be compared |
206 |
> |
* with the keys currently in the map |
207 |
> |
* @throws NullPointerException if the specified key is null |
208 |
> |
* and this map uses natural ordering, or its comparator |
209 |
> |
* does not permit null keys |
210 |
|
*/ |
211 |
|
public boolean containsKey(Object key) { |
212 |
|
return getEntry(key) != null; |
217 |
|
* specified value. More formally, returns <tt>true</tt> if and only if |
218 |
|
* this map contains at least one mapping to a value <tt>v</tt> such |
219 |
|
* that <tt>(value==null ? v==null : value.equals(v))</tt>. This |
220 |
< |
* operation will probably require time linear in the Map size for most |
220 |
< |
* implementations of Map. |
220 |
> |
* operation requires time linear in the map size. |
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) { |
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 |
253 |
> |
* Returns the value to which this map maps the specified key, or |
254 |
> |
* <tt>null</tt> if the map contains no mapping for the 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> |
257 |
> |
* explicitly maps the key to <tt>null</tt>. The {@link #containsKey} |
258 |
|
* operation may be used to distinguish these two cases. |
259 |
|
* |
260 |
< |
* @param key key whose associated value is to be returned. |
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. |
268 |
< |
* |
269 |
< |
* @see #containsKey(Object) |
262 |
> |
* <tt>null</tt> if the map contains no mapping for the key |
263 |
> |
* @throws ClassCastException if the specified key cannot be compared |
264 |
> |
* with the keys currently in the map |
265 |
> |
* @throws NullPointerException if the specified key is null |
266 |
> |
* and this map uses natural ordering, or its comparator |
267 |
> |
* does not permit null keys |
268 |
|
*/ |
269 |
|
public V get(Object key) { |
270 |
|
Entry<K,V> p = getEntry(key); |
271 |
|
return (p==null ? null : p.value); |
272 |
|
} |
273 |
|
|
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 |
– |
*/ |
274 |
|
public Comparator<? super K> comparator() { |
275 |
|
return comparator; |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
< |
* 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. |
279 |
> |
* @throws NoSuchElementException {@inheritDoc} |
280 |
|
*/ |
281 |
|
public K firstKey() { |
282 |
|
return key(getFirstEntry()); |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
< |
* 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. |
286 |
> |
* @throws NoSuchElementException {@inheritDoc} |
287 |
|
*/ |
288 |
|
public K lastKey() { |
289 |
|
return key(getLastEntry()); |
290 |
|
} |
291 |
|
|
292 |
|
/** |
293 |
< |
* Copies all of the mappings from the specified map to this map. These |
294 |
< |
* mappings replace any mappings that this map had for any of the keys |
295 |
< |
* currently in the specified map. |
296 |
< |
* |
297 |
< |
* @param map mappings to be stored in this map. |
298 |
< |
* @throws ClassCastException class of a key or value in the specified |
299 |
< |
* map prevents it from being stored in this map. |
300 |
< |
* |
301 |
< |
* @throws NullPointerException if the given map is <tt>null</tt> or |
302 |
< |
* this map does not permit <tt>null</tt> keys and a |
318 |
< |
* key in the specified map is <tt>null</tt>. |
293 |
> |
* Copies all of the mappings from the specified map to this map. |
294 |
> |
* These mappings replace any mappings that this map had for any |
295 |
> |
* of the keys currently in the specified map. |
296 |
> |
* |
297 |
> |
* @param map mappings to be stored in this map |
298 |
> |
* @throws ClassCastException if the class of a key or value in |
299 |
> |
* the specified map prevents it from being stored in this map |
300 |
> |
* @throws NullPointerException if the specified map is null or |
301 |
> |
* the specified map contains a null key and this map does not |
302 |
> |
* permit null keys |
303 |
|
*/ |
304 |
|
public void putAll(Map<? extends K, ? extends V> map) { |
305 |
|
int mapSize = map.size(); |
324 |
|
* does not contain an entry for the key. |
325 |
|
* |
326 |
|
* @return this map's entry for the given key, or <tt>null</tt> if the map |
327 |
< |
* does not contain an entry for the key. |
328 |
< |
* @throws ClassCastException if the key cannot be compared with the keys |
329 |
< |
* currently in the map. |
330 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
331 |
< |
* natural order, or its comparator does not tolerate * |
332 |
< |
* <tt>null</tt> keys. |
327 |
> |
* does not contain an entry for the key |
328 |
> |
* @throws ClassCastException if the specified key cannot be compared |
329 |
> |
* with the keys currently in the map |
330 |
> |
* @throws NullPointerException if the specified key is null |
331 |
> |
* and this map uses natural ordering, or its comparator |
332 |
> |
* does not permit null keys |
333 |
|
*/ |
334 |
|
private Entry<K,V> getEntry(Object key) { |
335 |
|
// Offload comparator-based version for sake of performance |
336 |
|
if (comparator != null) |
337 |
|
return getEntryUsingComparator(key); |
338 |
< |
Comparable<K> k = (Comparable<K>) key; |
338 |
> |
Comparable<? super K> k = (Comparable<? super K>) key; |
339 |
|
Entry<K,V> p = root; |
340 |
|
while (p != null) { |
341 |
|
int cmp = k.compareTo(p.key); |
509 |
|
} |
510 |
|
|
511 |
|
/** |
512 |
< |
* Returns the key corresponding to the specified Entry. Throw |
513 |
< |
* NoSuchElementException if the Entry is <tt>null</tt>. |
512 |
> |
* Returns the key corresponding to the specified Entry. |
513 |
> |
* @throws NoSuchElementException if the Entry is null |
514 |
|
*/ |
515 |
|
private static <K> K key(Entry<K,?> e) { |
516 |
|
if (e==null) |
523 |
|
* If the map previously contained a mapping for this key, the old |
524 |
|
* value is replaced. |
525 |
|
* |
526 |
< |
* @param key key with which the specified value is to be associated. |
527 |
< |
* @param value value to be associated with the specified key. |
526 |
> |
* @param key key with which the specified value is to be associated |
527 |
> |
* @param value value to be associated with the specified key |
528 |
|
* |
529 |
< |
* @return the previous value associated with specified key, or <tt>null</tt> |
530 |
< |
* if there was no mapping for key. A <tt>null</tt> return can |
531 |
< |
* also indicate that the map previously associated <tt>null</tt> |
532 |
< |
* with the specified key. |
533 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
534 |
< |
* currently in the map. |
535 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
536 |
< |
* natural order, or its comparator does not tolerate |
537 |
< |
* <tt>null</tt> keys. |
529 |
> |
* @return the previous value associated with <tt>key</tt>, or |
530 |
> |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
531 |
> |
* (A <tt>null</tt> return can also indicate that the map |
532 |
> |
* previously associated <tt>null</tt> with <tt>key</tt>.) |
533 |
> |
* @throws ClassCastException if the specified key cannot be compared |
534 |
> |
* with the keys currently in the map |
535 |
> |
* @throws NullPointerException if the specified key is null |
536 |
> |
* and this map uses natural ordering, or its comparator |
537 |
> |
* does not permit null keys |
538 |
|
*/ |
539 |
|
public V put(K key, V value) { |
540 |
|
Entry<K,V> t = root; |
541 |
|
|
542 |
|
if (t == null) { |
543 |
+ |
if (key == null) { |
544 |
+ |
if (comparator == null) |
545 |
+ |
throw new NullPointerException(); |
546 |
+ |
comparator.compare(key, key); |
547 |
+ |
} |
548 |
|
incrementSize(); |
549 |
|
root = new Entry<K,V>(key, value, null); |
550 |
|
return null; |
551 |
< |
} |
551 |
> |
} |
552 |
|
|
553 |
|
while (true) { |
554 |
|
int cmp = compare(key, t.key); |
580 |
|
* Removes the mapping for this key from this TreeMap if present. |
581 |
|
* |
582 |
|
* @param key key for which mapping should be removed |
583 |
< |
* @return the previous value associated with specified key, or <tt>null</tt> |
584 |
< |
* if there was no mapping for key. A <tt>null</tt> return can |
585 |
< |
* also indicate that the map previously associated |
586 |
< |
* <tt>null</tt> with the specified key. |
587 |
< |
* |
588 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
589 |
< |
* currently in the map. |
590 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
591 |
< |
* natural order, or its comparator does not tolerate |
603 |
< |
* <tt>null</tt> keys. |
583 |
> |
* @return the previous value associated with <tt>key</tt>, or |
584 |
> |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
585 |
> |
* (A <tt>null</tt> return can also indicate that the map |
586 |
> |
* previously associated <tt>null</tt> with <tt>key</tt>.) |
587 |
> |
* @throws ClassCastException if the specified key cannot be compared |
588 |
> |
* with the keys currently in the map |
589 |
> |
* @throws NullPointerException if the specified key is null |
590 |
> |
* and this map uses natural ordering, or its comparator |
591 |
> |
* does not permit null keys |
592 |
|
*/ |
593 |
|
public V remove(Object key) { |
594 |
|
Entry<K,V> p = getEntry(key); |
601 |
|
} |
602 |
|
|
603 |
|
/** |
604 |
< |
* Removes all mappings from this TreeMap. |
604 |
> |
* Removes all of the mappings from this map. |
605 |
> |
* The map will be empty after this call returns. |
606 |
|
*/ |
607 |
|
public void clear() { |
608 |
|
modCount++; |
614 |
|
* Returns a shallow copy of this <tt>TreeMap</tt> instance. (The keys and |
615 |
|
* values themselves are not cloned.) |
616 |
|
* |
617 |
< |
* @return a shallow copy of this Map. |
617 |
> |
* @return a shallow copy of this map |
618 |
|
*/ |
619 |
|
public Object clone() { |
620 |
|
TreeMap<K,V> clone = null; |
644 |
|
|
645 |
|
// NavigableMap API methods |
646 |
|
|
658 |
– |
/** |
659 |
– |
* 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. |
664 |
– |
*/ |
647 |
|
public Map.Entry<K,V> firstEntry() { |
648 |
|
Entry<K,V> e = getFirstEntry(); |
649 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
649 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
650 |
|
} |
651 |
|
|
670 |
– |
/** |
671 |
– |
* 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. |
676 |
– |
*/ |
652 |
|
public Map.Entry<K,V> lastEntry() { |
653 |
|
Entry<K,V> e = getLastEntry(); |
654 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
654 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
655 |
|
} |
656 |
|
|
682 |
– |
/** |
683 |
– |
* 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. |
688 |
– |
*/ |
657 |
|
public Map.Entry<K,V> pollFirstEntry() { |
658 |
|
Entry<K,V> p = getFirstEntry(); |
659 |
< |
if (p == null) |
659 |
> |
if (p == null) |
660 |
|
return null; |
661 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(p); |
661 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(p); |
662 |
|
deleteEntry(p); |
663 |
|
return result; |
664 |
|
} |
665 |
|
|
698 |
– |
/** |
699 |
– |
* 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. |
704 |
– |
*/ |
666 |
|
public Map.Entry<K,V> pollLastEntry() { |
667 |
|
Entry<K,V> p = getLastEntry(); |
668 |
< |
if (p == null) |
668 |
> |
if (p == null) |
669 |
|
return null; |
670 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(p); |
670 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(p); |
671 |
|
deleteEntry(p); |
672 |
|
return result; |
673 |
|
} |
674 |
|
|
675 |
|
/** |
676 |
< |
* Returns a key-value mapping associated with the least key |
677 |
< |
* greater than or equal to the given key, or <tt>null</tt> if |
678 |
< |
* there is no such entry. |
679 |
< |
* |
719 |
< |
* @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. |
676 |
> |
* @throws ClassCastException {@inheritDoc} |
677 |
> |
* @throws NullPointerException if the specified key is null |
678 |
> |
* and this map uses natural ordering, or its comparator |
679 |
> |
* does not permit null keys |
680 |
|
*/ |
681 |
< |
public Map.Entry<K,V> ceilingEntry(K key) { |
682 |
< |
Entry<K,V> e = getCeilingEntry(key); |
683 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
681 |
> |
public Map.Entry<K,V> lowerEntry(K key) { |
682 |
> |
Entry<K,V> e = getLowerEntry(key); |
683 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
684 |
|
} |
685 |
|
|
733 |
– |
|
686 |
|
/** |
687 |
< |
* Returns least key greater than or equal to the given key, or |
688 |
< |
* <tt>null</tt> if there is no such key. |
689 |
< |
* |
690 |
< |
* @param key the key. |
739 |
< |
* @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. |
687 |
> |
* @throws ClassCastException {@inheritDoc} |
688 |
> |
* @throws NullPointerException if the specified key is null |
689 |
> |
* and this map uses natural ordering, or its comparator |
690 |
> |
* does not permit null keys |
691 |
|
*/ |
692 |
< |
public K ceilingKey(K key) { |
693 |
< |
Entry<K,V> e = getCeilingEntry(key); |
692 |
> |
public K lowerKey(K key) { |
693 |
> |
Entry<K,V> e = getLowerEntry(key); |
694 |
|
return (e == null)? null : e.key; |
695 |
|
} |
696 |
|
|
752 |
– |
|
753 |
– |
|
697 |
|
/** |
698 |
< |
* Returns a key-value mapping associated with the greatest key |
699 |
< |
* less than or equal to the given key, or <tt>null</tt> if there |
700 |
< |
* is no such entry. |
701 |
< |
* |
759 |
< |
* @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. |
698 |
> |
* @throws ClassCastException {@inheritDoc} |
699 |
> |
* @throws NullPointerException if the specified key is null |
700 |
> |
* and this map uses natural ordering, or its comparator |
701 |
> |
* does not permit null keys |
702 |
|
*/ |
703 |
|
public Map.Entry<K,V> floorEntry(K key) { |
704 |
|
Entry<K,V> e = getFloorEntry(key); |
705 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
705 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
706 |
|
} |
707 |
|
|
708 |
|
/** |
709 |
< |
* Returns the greatest key |
710 |
< |
* less than or equal to the given key, or <tt>null</tt> if there |
711 |
< |
* is no such key. |
712 |
< |
* |
778 |
< |
* @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. |
709 |
> |
* @throws ClassCastException {@inheritDoc} |
710 |
> |
* @throws NullPointerException if the specified key is null |
711 |
> |
* and this map uses natural ordering, or its comparator |
712 |
> |
* does not permit null keys |
713 |
|
*/ |
714 |
|
public K floorKey(K key) { |
715 |
|
Entry<K,V> e = getFloorEntry(key); |
717 |
|
} |
718 |
|
|
719 |
|
/** |
720 |
< |
* Returns a key-value mapping associated with the least key |
721 |
< |
* strictly greater than the given key, or <tt>null</tt> if there |
722 |
< |
* is no such entry. |
723 |
< |
* |
797 |
< |
* @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. |
720 |
> |
* @throws ClassCastException {@inheritDoc} |
721 |
> |
* @throws NullPointerException if the specified key is null |
722 |
> |
* and this map uses natural ordering, or its comparator |
723 |
> |
* does not permit null keys |
724 |
|
*/ |
725 |
< |
public Map.Entry<K,V> higherEntry(K key) { |
726 |
< |
Entry<K,V> e = getHigherEntry(key); |
727 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
725 |
> |
public Map.Entry<K,V> ceilingEntry(K key) { |
726 |
> |
Entry<K,V> e = getCeilingEntry(key); |
727 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
728 |
|
} |
729 |
|
|
730 |
|
/** |
731 |
< |
* Returns the least key strictly greater than the given key, or |
732 |
< |
* <tt>null</tt> if there is no such key. |
733 |
< |
* |
734 |
< |
* @param key the key. |
816 |
< |
* @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. |
731 |
> |
* @throws ClassCastException {@inheritDoc} |
732 |
> |
* @throws NullPointerException if the specified key is null |
733 |
> |
* and this map uses natural ordering, or its comparator |
734 |
> |
* does not permit null keys |
735 |
|
*/ |
736 |
< |
public K higherKey(K key) { |
737 |
< |
Entry<K,V> e = getHigherEntry(key); |
736 |
> |
public K ceilingKey(K key) { |
737 |
> |
Entry<K,V> e = getCeilingEntry(key); |
738 |
|
return (e == null)? null : e.key; |
739 |
|
} |
740 |
|
|
741 |
|
/** |
742 |
< |
* Returns a key-value mapping associated with the greatest |
743 |
< |
* key strictly less than the given key, or <tt>null</tt> if there is no |
744 |
< |
* such entry. |
745 |
< |
* |
834 |
< |
* @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. |
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 |
|
*/ |
747 |
< |
public Map.Entry<K,V> lowerEntry(K key) { |
748 |
< |
Entry<K,V> e = getLowerEntry(key); |
749 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
747 |
> |
public Map.Entry<K,V> higherEntry(K key) { |
748 |
> |
Entry<K,V> e = getHigherEntry(key); |
749 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
750 |
|
} |
751 |
|
|
752 |
|
/** |
753 |
< |
* Returns the greatest key strictly less than the given key, or |
754 |
< |
* <tt>null</tt> if there is no such key. |
755 |
< |
* |
756 |
< |
* @param key the key. |
853 |
< |
* @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. |
753 |
> |
* @throws ClassCastException {@inheritDoc} |
754 |
> |
* @throws NullPointerException if the specified key is null |
755 |
> |
* and this map uses natural ordering, or its comparator |
756 |
> |
* does not permit null keys |
757 |
|
*/ |
758 |
< |
public K lowerKey(K key) { |
759 |
< |
Entry<K,V> e = getLowerEntry(key); |
758 |
> |
public K higherKey(K key) { |
759 |
> |
Entry<K,V> e = getHigherEntry(key); |
760 |
|
return (e == null)? null : e.key; |
761 |
|
} |
762 |
|
|
769 |
|
*/ |
770 |
|
private transient Set<Map.Entry<K,V>> entrySet = null; |
771 |
|
private transient Set<Map.Entry<K,V>> descendingEntrySet = null; |
772 |
< |
private transient Set<K> descendingKeySet = null; |
876 |
< |
|
877 |
< |
transient Set<K> keySet = null; // XXX remove when integrated |
878 |
< |
transient Collection<V> values = null; // XXX remove when integrated |
772 |
> |
private transient Set<K> descendingKeySet = null; |
773 |
|
|
774 |
|
/** |
775 |
< |
* Returns a Set view of the keys contained in this map. The set's |
776 |
< |
* iterator will return the keys in ascending order. The set is backed by |
777 |
< |
* this <tt>TreeMap</tt> instance, so changes to this map are reflected in |
778 |
< |
* the Set, and vice-versa. The Set supports element removal, which |
779 |
< |
* removes the corresponding mapping from the map, via the |
780 |
< |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>, |
781 |
< |
* <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not support |
782 |
< |
* the <tt>add</tt> or <tt>addAll</tt> operations. |
783 |
< |
* |
784 |
< |
* @return a set view of the keys contained in this TreeMap. |
775 |
> |
* Returns a {@link Set} view of the keys contained in this map. |
776 |
> |
* The set's iterator returns the keys in ascending order. |
777 |
> |
* The set is backed by the map, so changes to the map are |
778 |
> |
* reflected in the set, and vice-versa. If the map is modified |
779 |
> |
* while an iteration over the set is in progress (except through |
780 |
> |
* the iterator's own <tt>remove</tt> operation), the results of |
781 |
> |
* the iteration are undefined. The set supports element removal, |
782 |
> |
* which removes the corresponding mapping from the map, via the |
783 |
> |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
784 |
> |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
785 |
> |
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt> |
786 |
> |
* operations. |
787 |
|
*/ |
788 |
|
public Set<K> keySet() { |
789 |
|
Set<K> ks = keySet; |
794 |
|
public Iterator<K> iterator() { |
795 |
|
return new KeyIterator(getFirstEntry()); |
796 |
|
} |
797 |
< |
|
797 |
> |
|
798 |
|
public int size() { |
799 |
|
return TreeMap.this.size(); |
800 |
|
} |
801 |
< |
|
801 |
> |
|
802 |
|
public boolean contains(Object o) { |
803 |
|
return containsKey(o); |
804 |
|
} |
805 |
< |
|
805 |
> |
|
806 |
|
public boolean remove(Object o) { |
807 |
|
int oldSize = size; |
808 |
|
TreeMap.this.remove(o); |
809 |
|
return size != oldSize; |
810 |
|
} |
811 |
< |
|
811 |
> |
|
812 |
|
public void clear() { |
813 |
|
TreeMap.this.clear(); |
814 |
|
} |
815 |
|
} |
816 |
|
|
817 |
|
/** |
818 |
< |
* Returns a collection view of the values contained in this map. The |
819 |
< |
* collection's iterator will return the values in the order that their |
820 |
< |
* corresponding keys appear in the tree. The collection is backed by |
821 |
< |
* this <tt>TreeMap</tt> instance, so changes to this map are reflected in |
822 |
< |
* the collection, and vice-versa. The collection supports element |
823 |
< |
* removal, which removes the corresponding mapping from the map through |
824 |
< |
* the <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
825 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
826 |
< |
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
827 |
< |
* |
828 |
< |
* @return a collection view of the values contained in this map. |
818 |
> |
* Returns a {@link Collection} view of the values contained in this map. |
819 |
> |
* The collection's iterator returns the values in ascending order |
820 |
> |
* of the corresponding keys. |
821 |
> |
* The collection is backed by the map, so changes to the map are |
822 |
> |
* reflected in the collection, and vice-versa. If the map is |
823 |
> |
* modified while an iteration over the collection is in progress |
824 |
> |
* (except through the iterator's own <tt>remove</tt> operation), |
825 |
> |
* the results of the iteration are undefined. The collection |
826 |
> |
* supports element removal, which removes the corresponding |
827 |
> |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
828 |
> |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
829 |
> |
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not |
830 |
> |
* support the <tt>add</tt> or <tt>addAll</tt> operations. |
831 |
|
*/ |
832 |
|
public Collection<V> values() { |
833 |
|
Collection<V> vs = values; |
838 |
|
public Iterator<V> iterator() { |
839 |
|
return new ValueIterator(getFirstEntry()); |
840 |
|
} |
841 |
< |
|
841 |
> |
|
842 |
|
public int size() { |
843 |
|
return TreeMap.this.size(); |
844 |
|
} |
845 |
< |
|
845 |
> |
|
846 |
|
public boolean contains(Object o) { |
847 |
|
for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) |
848 |
|
if (valEquals(e.getValue(), o)) |
849 |
|
return true; |
850 |
|
return false; |
851 |
|
} |
852 |
< |
|
852 |
> |
|
853 |
|
public boolean remove(Object o) { |
854 |
|
for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) { |
855 |
|
if (valEquals(e.getValue(), o)) { |
859 |
|
} |
860 |
|
return false; |
861 |
|
} |
862 |
< |
|
862 |
> |
|
863 |
|
public void clear() { |
864 |
|
TreeMap.this.clear(); |
865 |
|
} |
866 |
|
} |
867 |
|
|
868 |
|
/** |
869 |
< |
* Returns a set view of the mappings contained in this map. The set's |
870 |
< |
* iterator returns the mappings in ascending key order. Each element in |
871 |
< |
* the returned set is a <tt>Map.Entry</tt>. The set is backed by this |
872 |
< |
* map, so changes to this map are reflected in the set, and vice-versa. |
873 |
< |
* The set supports element removal, which removes the corresponding |
874 |
< |
* mapping from the TreeMap, through the <tt>Iterator.remove</tt>, |
869 |
> |
* Returns a {@link Set} view of the mappings contained in this map. |
870 |
> |
* The set's iterator returns the entries in ascending key order. |
871 |
> |
* The set is backed by the map, so changes to the map are |
872 |
> |
* reflected in the set, and vice-versa. If the map is modified |
873 |
> |
* while an iteration over the set is in progress (except through |
874 |
> |
* the iterator's own <tt>remove</tt> operation, or through the |
875 |
> |
* <tt>setValue</tt> operation on a map entry returned by the |
876 |
> |
* iterator) the results of the iteration are undefined. The set |
877 |
> |
* supports element removal, which removes the corresponding |
878 |
> |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
879 |
|
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and |
880 |
< |
* <tt>clear</tt> operations. It does not support the <tt>add</tt> or |
881 |
< |
* <tt>addAll</tt> operations. |
980 |
< |
* |
981 |
< |
* @return a set view of the mappings contained in this map. |
982 |
< |
* @see Map.Entry |
880 |
> |
* <tt>clear</tt> operations. It does not support the |
881 |
> |
* <tt>add</tt> or <tt>addAll</tt> operations. |
882 |
|
*/ |
883 |
|
public Set<Map.Entry<K,V>> entrySet() { |
884 |
|
Set<Map.Entry<K,V>> es = entrySet; |
889 |
|
public Iterator<Map.Entry<K,V>> iterator() { |
890 |
|
return new EntryIterator(getFirstEntry()); |
891 |
|
} |
892 |
< |
|
892 |
> |
|
893 |
|
public boolean contains(Object o) { |
894 |
|
if (!(o instanceof Map.Entry)) |
895 |
|
return false; |
898 |
|
Entry<K,V> p = getEntry(entry.getKey()); |
899 |
|
return p != null && valEquals(p.getValue(), value); |
900 |
|
} |
901 |
< |
|
901 |
> |
|
902 |
|
public boolean remove(Object o) { |
903 |
|
if (!(o instanceof Map.Entry)) |
904 |
|
return false; |
911 |
|
} |
912 |
|
return false; |
913 |
|
} |
914 |
< |
|
914 |
> |
|
915 |
|
public int size() { |
916 |
|
return TreeMap.this.size(); |
917 |
|
} |
918 |
< |
|
918 |
> |
|
919 |
|
public void clear() { |
920 |
|
TreeMap.this.clear(); |
921 |
|
} |
922 |
|
} |
923 |
|
|
1025 |
– |
/** |
1026 |
– |
* Returns a set view of the mappings contained in this map. The |
1027 |
– |
* set's iterator returns the mappings in descending key order. |
1028 |
– |
* Each element in the returned set is a <tt>Map.Entry</tt>. The |
1029 |
– |
* set is backed by this map, so changes to this map are reflected |
1030 |
– |
* in the set, and vice-versa. The set supports element removal, |
1031 |
– |
* which removes the corresponding mapping from the TreeMap, |
1032 |
– |
* through the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
1033 |
– |
* <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> |
1034 |
– |
* operations. It does not support the <tt>add</tt> or |
1035 |
– |
* <tt>addAll</tt> operations. |
1036 |
– |
* |
1037 |
– |
* @return a set view of the mappings contained in this map, in |
1038 |
– |
* descending key order |
1039 |
– |
* @see Map.Entry |
1040 |
– |
*/ |
924 |
|
public Set<Map.Entry<K,V>> descendingEntrySet() { |
925 |
|
Set<Map.Entry<K,V>> es = descendingEntrySet; |
926 |
|
return (es != null) ? es : (descendingEntrySet = new DescendingEntrySet()); |
932 |
|
} |
933 |
|
} |
934 |
|
|
1052 |
– |
/** |
1053 |
– |
* Returns a Set view of the keys contained in this map. The |
1054 |
– |
* set's iterator will return the keys in descending order. The |
1055 |
– |
* map is backed by this <tt>TreeMap</tt> instance, so changes to |
1056 |
– |
* this map are reflected in the Set, and vice-versa. The Set |
1057 |
– |
* supports element removal, which removes the corresponding |
1058 |
– |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
1059 |
– |
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, |
1060 |
– |
* and <tt>clear</tt> operations. It does not support the |
1061 |
– |
* <tt>add</tt> or <tt>addAll</tt> operations. |
1062 |
– |
* |
1063 |
– |
* @return a set view of the keys contained in this TreeMap. |
1064 |
– |
*/ |
935 |
|
public Set<K> descendingKeySet() { |
936 |
|
Set<K> ks = descendingKeySet; |
937 |
|
return (ks != null) ? ks : (descendingKeySet = new DescendingKeySet()); |
944 |
|
} |
945 |
|
|
946 |
|
/** |
947 |
< |
* Returns a view of the portion of this map whose keys range from |
1078 |
< |
* <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If |
1079 |
< |
* <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned |
1080 |
< |
* navigable map is empty.) The returned navigable map is backed |
1081 |
< |
* by this map, so changes in the returned navigable map are |
1082 |
< |
* reflected in this map, and vice-versa. The returned navigable |
1083 |
< |
* map supports all optional map operations.<p> |
1084 |
< |
* |
1085 |
< |
* The navigable map returned by this method will throw an |
1086 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1087 |
< |
* less than <tt>fromKey</tt> or greater than or equal to |
1088 |
< |
* <tt>toKey</tt>.<p> |
1089 |
< |
* |
1090 |
< |
* Note: this method always returns a <i>half-open range</i> (which |
1091 |
< |
* includes its low endpoint but not its high endpoint). If you need a |
1092 |
< |
* <i>closed range</i> (which includes both endpoints), and the key type |
1093 |
< |
* allows for calculation of the successor of a given key, merely request the |
1094 |
< |
* subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>. |
1095 |
< |
* For example, suppose that <tt>m</tt> is a navigable map whose keys are |
1096 |
< |
* strings. The following idiom obtains a view containing all of the |
1097 |
< |
* key-value mappings in <tt>m</tt> whose keys are between <tt>low</tt> |
1098 |
< |
* and <tt>high</tt>, inclusive: |
1099 |
< |
* <pre> NavigableMap sub = m.navigableSubMap(low, high+"\0");</pre> |
1100 |
< |
* A similar technique can be used to generate an <i>open range</i> (which |
1101 |
< |
* contains neither endpoint). The following idiom obtains a view |
1102 |
< |
* containing all of the key-value mappings in <tt>m</tt> whose keys are |
1103 |
< |
* between <tt>low</tt> and <tt>high</tt>, exclusive: |
1104 |
< |
* <pre> NavigableMap sub = m.navigableSubMap(low+"\0", high);</pre> |
1105 |
< |
* |
1106 |
< |
* @param fromKey low endpoint (inclusive) of the subMap. |
1107 |
< |
* @param toKey high endpoint (exclusive) of the subMap. |
1108 |
< |
* |
1109 |
< |
* @return a view of the portion of this map whose keys range from |
1110 |
< |
* <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. |
1111 |
< |
* |
1112 |
< |
* @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt> |
1113 |
< |
* cannot be compared to one another using this map's comparator |
1114 |
< |
* (or, if the map has no comparator, using natural ordering). |
1115 |
< |
* @throws IllegalArgumentException if <tt>fromKey</tt> is greater than |
1116 |
< |
* <tt>toKey</tt>. |
947 |
> |
* @throws ClassCastException {@inheritDoc} |
948 |
|
* @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is |
949 |
< |
* <tt>null</tt> and this map uses natural order, or its |
950 |
< |
* comparator does not tolerate <tt>null</tt> keys. |
949 |
> |
* null and this map uses natural ordering, or its comparator |
950 |
> |
* does not permit null keys |
951 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
952 |
|
*/ |
953 |
|
public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) { |
954 |
|
return new SubMap(fromKey, toKey); |
955 |
|
} |
956 |
|
|
1125 |
– |
|
957 |
|
/** |
958 |
< |
* Returns a view of the portion of this map whose keys are strictly less |
959 |
< |
* than <tt>toKey</tt>. The returned navigable map is backed by this map, so |
960 |
< |
* changes in the returned navigable map are reflected in this map, and |
961 |
< |
* vice-versa. The returned navigable map supports all optional map |
962 |
< |
* operations.<p> |
1132 |
< |
* |
1133 |
< |
* The navigable map returned by this method will throw an |
1134 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1135 |
< |
* greater than or equal to <tt>toKey</tt>.<p> |
1136 |
< |
* |
1137 |
< |
* Note: this method always returns a view that does not contain its |
1138 |
< |
* (high) endpoint. If you need a view that does contain this endpoint, |
1139 |
< |
* and the key type allows for calculation of the successor of a given key, |
1140 |
< |
* merely request a headMap bounded by <tt>successor(highEndpoint)</tt>. |
1141 |
< |
* For example, suppose that suppose that <tt>m</tt> is a navigable map whose |
1142 |
< |
* keys are strings. The following idiom obtains a view containing all of |
1143 |
< |
* the key-value mappings in <tt>m</tt> whose keys are less than or equal |
1144 |
< |
* to <tt>high</tt>: |
1145 |
< |
* <pre> |
1146 |
< |
* NavigableMap head = m.navigableHeadMap(high+"\0"); |
1147 |
< |
* </pre> |
1148 |
< |
* |
1149 |
< |
* @param toKey high endpoint (exclusive) of the headMap. |
1150 |
< |
* @return a view of the portion of this map whose keys are strictly |
1151 |
< |
* less than <tt>toKey</tt>. |
1152 |
< |
* |
1153 |
< |
* @throws ClassCastException if <tt>toKey</tt> is not compatible |
1154 |
< |
* with this map's comparator (or, if the map has no comparator, |
1155 |
< |
* if <tt>toKey</tt> does not implement <tt>Comparable</tt>). |
1156 |
< |
* @throws IllegalArgumentException if this map is itself a subMap, |
1157 |
< |
* headMap, or tailMap, and <tt>toKey</tt> is not within the |
1158 |
< |
* specified range of the subMap, headMap, or tailMap. |
1159 |
< |
* @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and |
1160 |
< |
* this map uses natural order, or its comparator does not |
1161 |
< |
* tolerate <tt>null</tt> keys. |
958 |
> |
* @throws ClassCastException {@inheritDoc} |
959 |
> |
* @throws NullPointerException if <tt>toKey</tt> is null |
960 |
> |
* and this map uses natural ordering, or its comparator |
961 |
> |
* does not permit null keys |
962 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
963 |
|
*/ |
964 |
|
public NavigableMap<K,V> navigableHeadMap(K toKey) { |
965 |
|
return new SubMap(toKey, true); |
966 |
|
} |
967 |
|
|
968 |
|
/** |
969 |
< |
* Returns a view of the portion of this map whose keys are greater than |
970 |
< |
* or equal to <tt>fromKey</tt>. The returned navigable map is backed by |
971 |
< |
* this map, so changes in the returned navigable map are reflected in this |
972 |
< |
* map, and vice-versa. The returned navigable map supports all optional map |
973 |
< |
* operations.<p> |
1173 |
< |
* |
1174 |
< |
* The navigable map returned by this method will throw an |
1175 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1176 |
< |
* less than <tt>fromKey</tt>.<p> |
1177 |
< |
* |
1178 |
< |
* Note: this method always returns a view that contains its (low) |
1179 |
< |
* endpoint. If you need a view that does not contain this endpoint, and |
1180 |
< |
* the element type allows for calculation of the successor of a given value, |
1181 |
< |
* merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>. |
1182 |
< |
* For example, suppose that <tt>m</tt> is a navigable map whose keys |
1183 |
< |
* are strings. The following idiom obtains a view containing |
1184 |
< |
* all of the key-value mappings in <tt>m</tt> whose keys are strictly |
1185 |
< |
* greater than <tt>low</tt>: <pre> |
1186 |
< |
* NavigableMap tail = m.navigableTailMap(low+"\0"); |
1187 |
< |
* </pre> |
1188 |
< |
* |
1189 |
< |
* @param fromKey low endpoint (inclusive) of the tailMap. |
1190 |
< |
* @return a view of the portion of this map whose keys are greater |
1191 |
< |
* than or equal to <tt>fromKey</tt>. |
1192 |
< |
* @throws ClassCastException if <tt>fromKey</tt> is not compatible |
1193 |
< |
* with this map's comparator (or, if the map has no comparator, |
1194 |
< |
* if <tt>fromKey</tt> does not implement <tt>Comparable</tt>). |
1195 |
< |
* @throws IllegalArgumentException if this map is itself a subMap, |
1196 |
< |
* headMap, or tailMap, and <tt>fromKey</tt> is not within the |
1197 |
< |
* specified range of the subMap, headMap, or tailMap. |
1198 |
< |
* @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and |
1199 |
< |
* this map uses natural order, or its comparator does not |
1200 |
< |
* tolerate <tt>null</tt> keys. |
969 |
> |
* @throws ClassCastException {@inheritDoc} |
970 |
> |
* @throws NullPointerException if <tt>fromKey</tt> is null |
971 |
> |
* and this map uses natural ordering, or its comparator |
972 |
> |
* does not permit null keys |
973 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
974 |
|
*/ |
975 |
|
public NavigableMap<K,V> navigableTailMap(K fromKey) { |
976 |
|
return new SubMap(fromKey, false); |
977 |
|
} |
978 |
|
|
979 |
|
/** |
980 |
< |
* Equivalent to <tt>navigableSubMap</tt> but with a return |
981 |
< |
* type conforming to the <tt>SortedMap</tt> interface. |
982 |
< |
* @param fromKey low endpoint (inclusive) of the subMap. |
983 |
< |
* @param toKey high endpoint (exclusive) of the subMap. |
984 |
< |
* |
985 |
< |
* @return a view of the portion of this map whose keys range from |
1213 |
< |
* <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. |
1214 |
< |
* |
1215 |
< |
* @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt> |
1216 |
< |
* cannot be compared to one another using this map's comparator |
1217 |
< |
* (or, if the map has no comparator, using natural ordering). |
1218 |
< |
* @throws IllegalArgumentException if <tt>fromKey</tt> is greater than |
1219 |
< |
* <tt>toKey</tt>. |
980 |
> |
* Equivalent to {@link #navigableSubMap} but with a return type |
981 |
> |
* conforming to the <tt>SortedMap</tt> interface. |
982 |
> |
* |
983 |
> |
* <p>{@inheritDoc} |
984 |
> |
* |
985 |
> |
* @throws ClassCastException {@inheritDoc} |
986 |
|
* @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is |
987 |
< |
* <tt>null</tt> and this map uses natural order, or its |
988 |
< |
* comparator does not tolerate <tt>null</tt> keys. |
987 |
> |
* null and this map uses natural ordering, or its comparator |
988 |
> |
* does not permit null keys |
989 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
990 |
|
*/ |
991 |
|
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
992 |
|
return new SubMap(fromKey, toKey); |
993 |
|
} |
994 |
|
|
1228 |
– |
|
995 |
|
/** |
996 |
< |
* Equivalent to <tt>navigableHeadMap</tt> but with a return |
997 |
< |
* type conforming to the <tt>SortedMap</tt> interface. |
996 |
> |
* Equivalent to {@link #navigableHeadMap} but with a return type |
997 |
> |
* conforming to the <tt>SortedMap</tt> interface. |
998 |
> |
* |
999 |
> |
* <p>{@inheritDoc} |
1000 |
|
* |
1001 |
< |
* @param toKey high endpoint (exclusive) of the headMap. |
1002 |
< |
* @return a view of the portion of this map whose keys are strictly |
1003 |
< |
* less than <tt>toKey</tt>. |
1004 |
< |
* |
1005 |
< |
* @throws ClassCastException if <tt>toKey</tt> is not compatible |
1238 |
< |
* with this map's comparator (or, if the map has no comparator, |
1239 |
< |
* if <tt>toKey</tt> does not implement <tt>Comparable</tt>). |
1240 |
< |
* @throws IllegalArgumentException if this map is itself a subMap, |
1241 |
< |
* headMap, or tailMap, and <tt>toKey</tt> is not within the |
1242 |
< |
* specified range of the subMap, headMap, or tailMap. |
1243 |
< |
* @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and |
1244 |
< |
* this map uses natural order, or its comparator does not |
1245 |
< |
* tolerate <tt>null</tt> keys. |
1001 |
> |
* @throws ClassCastException {@inheritDoc} |
1002 |
> |
* @throws NullPointerException if <tt>toKey</tt> is null |
1003 |
> |
* and this map uses natural ordering, or its comparator |
1004 |
> |
* does not permit null keys |
1005 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
1006 |
|
*/ |
1007 |
|
public SortedMap<K,V> headMap(K toKey) { |
1008 |
|
return new SubMap(toKey, true); |
1009 |
|
} |
1010 |
|
|
1011 |
|
/** |
1012 |
< |
* Equivalent to <tt>navigableTailMap</tt> but with a return |
1013 |
< |
* type conforming to the <tt>SortedMap</tt> interface. |
1012 |
> |
* Equivalent to {@link #navigableTailMap} but with a return type |
1013 |
> |
* conforming to the <tt>SortedMap</tt> interface. |
1014 |
|
* |
1015 |
< |
* @param fromKey low endpoint (inclusive) of the tailMap. |
1016 |
< |
* @return a view of the portion of this map whose keys are greater |
1017 |
< |
* than or equal to <tt>fromKey</tt>. |
1018 |
< |
* @throws ClassCastException if <tt>fromKey</tt> is not compatible |
1019 |
< |
* with this map's comparator (or, if the map has no comparator, |
1020 |
< |
* if <tt>fromKey</tt> does not implement <tt>Comparable</tt>). |
1021 |
< |
* @throws IllegalArgumentException if this map is itself a subMap, |
1262 |
< |
* headMap, or tailMap, and <tt>fromKey</tt> is not within the |
1263 |
< |
* specified range of the subMap, headMap, or tailMap. |
1264 |
< |
* @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and |
1265 |
< |
* this map uses natural order, or its comparator does not |
1266 |
< |
* tolerate <tt>null</tt> keys. |
1015 |
> |
* <p>{@inheritDoc} |
1016 |
> |
* |
1017 |
> |
* @throws ClassCastException {@inheritDoc} |
1018 |
> |
* @throws NullPointerException if <tt>fromKey</tt> is null |
1019 |
> |
* and this map uses natural ordering, or its comparator |
1020 |
> |
* does not permit null keys |
1021 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
1022 |
|
*/ |
1023 |
|
public SortedMap<K,V> tailMap(K fromKey) { |
1024 |
|
return new SubMap(fromKey, false); |
1067 |
|
} |
1068 |
|
|
1069 |
|
public boolean containsKey(Object key) { |
1070 |
< |
return inRange((K) key) && TreeMap.this.containsKey(key); |
1070 |
> |
return inRange(key) && TreeMap.this.containsKey(key); |
1071 |
|
} |
1072 |
|
|
1073 |
|
public V get(Object key) { |
1074 |
< |
if (!inRange((K) key)) |
1074 |
> |
if (!inRange(key)) |
1075 |
|
return null; |
1076 |
|
return TreeMap.this.get(key); |
1077 |
|
} |
1083 |
|
} |
1084 |
|
|
1085 |
|
public V remove(Object key) { |
1086 |
< |
if (!inRange((K) key)) |
1086 |
> |
if (!inRange(key)) |
1087 |
|
return null; |
1088 |
|
return TreeMap.this.remove(key); |
1089 |
|
} |
1096 |
|
TreeMap.Entry<K,V> e = fromStart ? getFirstEntry() : getCeilingEntry(fromKey); |
1097 |
|
K first = key(e); |
1098 |
|
if (!toEnd && compare(first, toKey) >= 0) |
1099 |
< |
throw(new NoSuchElementException()); |
1099 |
> |
throw new NoSuchElementException(); |
1100 |
|
return first; |
1101 |
|
} |
1102 |
|
|
1104 |
|
TreeMap.Entry<K,V> e = toEnd ? getLastEntry() : getLowerEntry(toKey); |
1105 |
|
K last = key(e); |
1106 |
|
if (!fromStart && compare(last, fromKey) < 0) |
1107 |
< |
throw(new NoSuchElementException()); |
1107 |
> |
throw new NoSuchElementException(); |
1108 |
|
return last; |
1109 |
|
} |
1110 |
|
|
1111 |
|
public Map.Entry<K,V> firstEntry() { |
1112 |
< |
TreeMap.Entry<K,V> e = fromStart ? |
1112 |
> |
TreeMap.Entry<K,V> e = fromStart ? |
1113 |
|
getFirstEntry() : getCeilingEntry(fromKey); |
1114 |
|
if (e == null || (!toEnd && compare(e.key, toKey) >= 0)) |
1115 |
|
return null; |
1117 |
|
} |
1118 |
|
|
1119 |
|
public Map.Entry<K,V> lastEntry() { |
1120 |
< |
TreeMap.Entry<K,V> e = toEnd ? |
1120 |
> |
TreeMap.Entry<K,V> e = toEnd ? |
1121 |
|
getLastEntry() : getLowerEntry(toKey); |
1122 |
|
if (e == null || (!fromStart && compare(e.key, fromKey) < 0)) |
1123 |
|
return null; |
1125 |
|
} |
1126 |
|
|
1127 |
|
public Map.Entry<K,V> pollFirstEntry() { |
1128 |
< |
TreeMap.Entry<K,V> e = fromStart ? |
1128 |
> |
TreeMap.Entry<K,V> e = fromStart ? |
1129 |
|
getFirstEntry() : getCeilingEntry(fromKey); |
1130 |
|
if (e == null || (!toEnd && compare(e.key, toKey) >= 0)) |
1131 |
|
return null; |
1132 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(e); |
1132 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1133 |
|
deleteEntry(e); |
1134 |
|
return result; |
1135 |
|
} |
1136 |
|
|
1137 |
|
public Map.Entry<K,V> pollLastEntry() { |
1138 |
< |
TreeMap.Entry<K,V> e = toEnd ? |
1138 |
> |
TreeMap.Entry<K,V> e = toEnd ? |
1139 |
|
getLastEntry() : getLowerEntry(toKey); |
1140 |
|
if (e == null || (!fromStart && compare(e.key, fromKey) < 0)) |
1141 |
|
return null; |
1142 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(e); |
1142 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1143 |
|
deleteEntry(e); |
1144 |
|
return result; |
1145 |
|
} |
1154 |
|
|
1155 |
|
public Map.Entry<K,V> ceilingEntry(K key) { |
1156 |
|
TreeMap.Entry<K,V> e = subceiling(key); |
1157 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1157 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1158 |
|
} |
1159 |
|
|
1160 |
|
public K ceilingKey(K key) { |
1173 |
|
|
1174 |
|
public Map.Entry<K,V> higherEntry(K key) { |
1175 |
|
TreeMap.Entry<K,V> e = subhigher(key); |
1176 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1176 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1177 |
|
} |
1178 |
|
|
1179 |
|
public K higherKey(K key) { |
1191 |
|
|
1192 |
|
public Map.Entry<K,V> floorEntry(K key) { |
1193 |
|
TreeMap.Entry<K,V> e = subfloor(key); |
1194 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1194 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1195 |
|
} |
1196 |
|
|
1197 |
|
public K floorKey(K key) { |
1209 |
|
|
1210 |
|
public Map.Entry<K,V> lowerEntry(K key) { |
1211 |
|
TreeMap.Entry<K,V> e = sublower(key); |
1212 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1212 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1213 |
|
} |
1214 |
|
|
1215 |
|
public K lowerKey(K key) { |
1278 |
|
} |
1279 |
|
|
1280 |
|
private transient Set<Map.Entry<K,V>> descendingEntrySetView = null; |
1281 |
< |
private transient Set<K> descendingKeySetView = null; |
1281 |
> |
private transient Set<K> descendingKeySetView = null; |
1282 |
|
|
1283 |
|
public Set<Map.Entry<K,V>> descendingEntrySet() { |
1284 |
|
Set<Map.Entry<K,V>> es = descendingEntrySetView; |
1285 |
< |
return (es != null) ? es : (descendingEntrySetView = new DescendingEntrySetView()); |
1285 |
> |
return (es != null) ? es : |
1286 |
> |
(descendingEntrySetView = new DescendingEntrySetView()); |
1287 |
|
} |
1288 |
|
|
1289 |
|
public Set<K> descendingKeySet() { |
1290 |
|
Set<K> ks = descendingKeySetView; |
1291 |
< |
return (ks != null) ? ks : (descendingKeySetView = new DescendingKeySetView()); |
1291 |
> |
return (ks != null) ? ks : |
1292 |
> |
(descendingKeySetView = new DescendingKeySetView()); |
1293 |
|
} |
1294 |
|
|
1295 |
|
private class DescendingEntrySetView extends EntrySetView { |
1304 |
|
public Iterator<K> iterator() { |
1305 |
|
return new Iterator<K>() { |
1306 |
|
private Iterator<Entry<K,V>> i = descendingEntrySet().iterator(); |
1307 |
< |
|
1307 |
> |
|
1308 |
|
public boolean hasNext() { return i.hasNext(); } |
1309 |
|
public K next() { return i.next().getKey(); } |
1310 |
|
public void remove() { i.remove(); } |
1311 |
|
}; |
1312 |
|
} |
1313 |
< |
|
1313 |
> |
|
1314 |
|
public int size() { |
1315 |
|
return SubMap.this.size(); |
1316 |
|
} |
1317 |
< |
|
1317 |
> |
|
1318 |
|
public boolean contains(Object k) { |
1319 |
|
return SubMap.this.containsKey(k); |
1320 |
|
} |
1321 |
|
} |
1322 |
|
|
1566 |
– |
|
1323 |
|
public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) { |
1324 |
|
if (!inRange2(fromKey)) |
1325 |
|
throw new IllegalArgumentException("fromKey out of range"); |
1340 |
|
return new SubMap(false, fromKey, toEnd, toKey); |
1341 |
|
} |
1342 |
|
|
1587 |
– |
|
1343 |
|
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
1344 |
|
return navigableSubMap(fromKey, toKey); |
1345 |
|
} |
1352 |
|
return navigableTailMap(fromKey); |
1353 |
|
} |
1354 |
|
|
1355 |
< |
private boolean inRange(K key) { |
1355 |
> |
private boolean inRange(Object key) { |
1356 |
|
return (fromStart || compare(key, fromKey) >= 0) && |
1357 |
|
(toEnd || compare(key, toKey) < 0); |
1358 |
|
} |
1359 |
|
|
1360 |
|
// This form allows the high endpoint (as well as all legit keys) |
1361 |
< |
private boolean inRange2(K key) { |
1361 |
> |
private boolean inRange2(Object key) { |
1362 |
|
return (fromStart || compare(key, fromKey) >= 0) && |
1363 |
|
(toEnd || compare(key, toKey) <= 0); |
1364 |
|
} |
1380 |
|
return next != null; |
1381 |
|
} |
1382 |
|
|
1383 |
< |
Entry<K,V> nextEntry() { |
1383 |
> |
Entry<K,V> nextEntry() { |
1384 |
|
if (next == null) |
1385 |
|
throw new NoSuchElementException(); |
1386 |
|
if (modCount != expectedModCount) |
1407 |
|
EntryIterator(Entry<K,V> first) { |
1408 |
|
super(first); |
1409 |
|
} |
1655 |
– |
|
1410 |
|
public Map.Entry<K,V> next() { |
1411 |
|
return nextEntry(); |
1412 |
|
} |
1451 |
|
} |
1452 |
|
} |
1453 |
|
|
1700 |
– |
|
1454 |
|
/** |
1455 |
|
* Base for Descending Iterators. |
1456 |
|
*/ |
1511 |
|
|
1512 |
|
} |
1513 |
|
|
1761 |
– |
|
1514 |
|
/** |
1515 |
|
* Compares two keys using the correct comparison method for this TreeMap. |
1516 |
|
*/ |
1517 |
< |
private int compare(K k1, K k2) { |
1518 |
< |
return (comparator==null ? ((Comparable</*-*/K>)k1).compareTo(k2) |
1519 |
< |
: comparator.compare((K)k1, (K)k2)); |
1517 |
> |
private int compare(Object k1, Object k2) { |
1518 |
> |
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2) |
1519 |
> |
: comparator.compare((K)k1, (K)k2); |
1520 |
|
} |
1521 |
|
|
1522 |
|
/** |
1523 |
< |
* Test two values for equality. Differs from o1.equals(o2) only in |
1523 |
> |
* Test two values for equality. Differs from o1.equals(o2) only in |
1524 |
|
* that it copes with <tt>null</tt> o1 properly. |
1525 |
|
*/ |
1526 |
|
private static boolean valEquals(Object o1, Object o2) { |
1556 |
|
/** |
1557 |
|
* Returns the key. |
1558 |
|
* |
1559 |
< |
* @return the key. |
1559 |
> |
* @return the key |
1560 |
|
*/ |
1561 |
|
public K getKey() { |
1562 |
|
return key; |
1565 |
|
/** |
1566 |
|
* Returns the value associated with the key. |
1567 |
|
* |
1568 |
< |
* @return the value associated with the key. |
1568 |
> |
* @return the value associated with the key |
1569 |
|
*/ |
1570 |
|
public V getValue() { |
1571 |
|
return value; |
1576 |
|
* value. |
1577 |
|
* |
1578 |
|
* @return the value associated with the key before this method was |
1579 |
< |
* called. |
1579 |
> |
* called |
1580 |
|
*/ |
1581 |
|
public V setValue(V value) { |
1582 |
|
V oldValue = this.value; |
1915 |
|
// Write out size (number of Mappings) |
1916 |
|
s.writeInt(size); |
1917 |
|
|
2166 |
– |
Set<Map.Entry<K,V>> es = entrySet(); |
1918 |
|
// Write out keys and values (alternating) |
1919 |
< |
for (Iterator<Map.Entry<K,V>> i = es.iterator(); i.hasNext(); ) { |
1919 |
> |
for (Iterator<Map.Entry<K,V>> i = entrySet().iterator(); i.hasNext(); ) { |
1920 |
|
Map.Entry<K,V> e = i.next(); |
1921 |
|
s.writeObject(e.getKey()); |
1922 |
|
s.writeObject(e.getValue()); |
1947 |
|
} |
1948 |
|
|
1949 |
|
/** Intended to be called only from TreeSet.addAll **/ |
1950 |
< |
void addAllForTreeSet(SortedSet<Map.Entry<K,V>> set, V defaultVal) { |
1950 |
> |
void addAllForTreeSet(SortedSet<? extends K> set, V defaultVal) { |
1951 |
|
try { |
1952 |
|
buildFromSorted(set.size(), set.iterator(), null, defaultVal); |
1953 |
|
} catch (java.io.IOException cannotHappen) { |
1972 |
|
* to calling this method. |
1973 |
|
* |
1974 |
|
* @param size the number of keys (or key-value pairs) to be read from |
1975 |
< |
* the iterator or stream. |
1975 |
> |
* the iterator or stream |
1976 |
|
* @param it If non-null, new entries are created from entries |
1977 |
|
* or keys read from this iterator. |
1978 |
|
* @param str If non-null, new entries are created from keys and |
2007 |
|
* @param level the current level of tree. Initial call should be 0. |
2008 |
|
* @param lo the first element index of this subtree. Initial should be 0. |
2009 |
|
* @param hi the last element index of this subtree. Initial should be |
2010 |
< |
* size-1. |
2010 |
> |
* size-1. |
2011 |
|
* @param redLevel the level at which nodes should be red. |
2012 |
|
* Must be equal to computeRedLevel for tree of this size. |
2013 |
|
*/ |
2091 |
|
level++; |
2092 |
|
return level; |
2093 |
|
} |
2343 |
– |
|
2094 |
|
} |