1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2006 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 |
|
|
10 |
|
/** |
11 |
< |
* Red-Black tree based implementation of the <tt>NavigableMap</tt> interface. |
12 |
< |
* This class guarantees that the map will be in ascending key order, sorted |
13 |
< |
* according to the <i>natural order</i> for the key's class (see |
14 |
< |
* <tt>Comparable</tt>), or by the comparator provided at creation time, |
16 |
< |
* depending on which constructor is used.<p> |
11 |
> |
* A Red-Black tree based {@link NavigableMap} implementation. |
12 |
> |
* The map is sorted according to the {@linkplain Comparable natural |
13 |
> |
* ordering} of its keys, or by a {@link Comparator} provided at map |
14 |
> |
* creation time, depending on which constructor is used. |
15 |
|
* |
16 |
< |
* This implementation provides guaranteed log(n) time cost for the |
16 |
> |
* <p>This implementation provides guaranteed log(n) time cost for the |
17 |
|
* <tt>containsKey</tt>, <tt>get</tt>, <tt>put</tt> and <tt>remove</tt> |
18 |
|
* operations. Algorithms are adaptations of those in Cormen, Leiserson, and |
19 |
< |
* Rivest's <I>Introduction to Algorithms</I>.<p> |
19 |
> |
* Rivest's <I>Introduction to Algorithms</I>. |
20 |
|
* |
21 |
< |
* Note that the ordering maintained by a sorted map (whether or not an |
21 |
> |
* <p>Note that the ordering maintained by a sorted map (whether or not an |
22 |
|
* explicit comparator is provided) must be <i>consistent with equals</i> if |
23 |
|
* this sorted map is to correctly implement the <tt>Map</tt> interface. (See |
24 |
|
* <tt>Comparable</tt> or <tt>Comparator</tt> for a precise definition of |
28 |
|
* method, so two keys that are deemed equal by this method are, from the |
29 |
|
* standpoint of the sorted map, equal. The behavior of a sorted map |
30 |
|
* <i>is</i> well-defined even if its ordering is inconsistent with equals; it |
31 |
< |
* just fails to obey the general contract of the <tt>Map</tt> interface.<p> |
31 |
> |
* just fails to obey the general contract of the <tt>Map</tt> interface. |
32 |
|
* |
33 |
< |
* <b>Note that this implementation is not synchronized.</b> If multiple |
34 |
< |
* threads access a map concurrently, and at least one of the threads modifies |
35 |
< |
* the map structurally, it <i>must</i> be synchronized externally. (A |
36 |
< |
* structural modification is any operation that adds or deletes one or more |
37 |
< |
* mappings; merely changing the value associated with an existing key is not |
38 |
< |
* a structural modification.) This is typically accomplished by |
39 |
< |
* synchronizing on some object that naturally encapsulates the map. If no |
40 |
< |
* such object exists, the map should be "wrapped" using the |
41 |
< |
* <tt>Collections.synchronizedMap</tt> method. This is best done at creation |
42 |
< |
* time, to prevent accidental unsynchronized access to the map: |
43 |
< |
* <pre> |
44 |
< |
* Map m = Collections.synchronizedMap(new TreeMap(...)); |
45 |
< |
* </pre><p> |
33 |
> |
* <p><strong>Note that this implementation is not synchronized.</strong> |
34 |
> |
* If multiple threads access a map concurrently, and at least one of the |
35 |
> |
* threads modifies the map structurally, it <i>must</i> be synchronized |
36 |
> |
* externally. (A structural modification is any operation that adds or |
37 |
> |
* deletes one or more mappings; merely changing the value associated |
38 |
> |
* with an existing key is not a structural modification.) This is |
39 |
> |
* typically accomplished by synchronizing on some object that naturally |
40 |
> |
* encapsulates the map. |
41 |
> |
* If no such object exists, the map should be "wrapped" using the |
42 |
> |
* {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap} |
43 |
> |
* method. This is best done at creation time, to prevent accidental |
44 |
> |
* unsynchronized access to the map: <pre> |
45 |
> |
* SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));</pre> |
46 |
|
* |
47 |
< |
* The iterators returned by all of this class's "collection view methods" are |
47 |
> |
* <p>The iterators returned by the <tt>iterator</tt> method of the collections |
48 |
> |
* returned by all of this class's "collection view methods" are |
49 |
|
* <i>fail-fast</i>: if the map is structurally modified at any time after the |
50 |
|
* iterator is created, in any way except through the iterator's own |
51 |
< |
* <tt>remove</tt> or <tt>add</tt> methods, the iterator throws a |
52 |
< |
* <tt>ConcurrentModificationException</tt>. Thus, in the face of concurrent |
51 |
> |
* <tt>remove</tt> method, the iterator will throw a {@link |
52 |
> |
* ConcurrentModificationException}. Thus, in the face of concurrent |
53 |
|
* modification, the iterator fails quickly and cleanly, rather than risking |
54 |
< |
* arbitrary, non-deterministic behavior at an undetermined time in the |
56 |
< |
* future. |
54 |
> |
* arbitrary, non-deterministic behavior at an undetermined time in the future. |
55 |
|
* |
56 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
57 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
71 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
72 |
|
* Java Collections Framework</a>. |
73 |
|
* |
74 |
+ |
* @param <K> the type of keys maintained by this map |
75 |
+ |
* @param <V> the type of mapped values |
76 |
+ |
* |
77 |
|
* @author Josh Bloch and Doug Lea |
78 |
|
* @version %I%, %G% |
79 |
|
* @see Map |
82 |
|
* @see Comparable |
83 |
|
* @see Comparator |
84 |
|
* @see Collection |
84 |
– |
* @see Collections#synchronizedMap(Map) |
85 |
|
* @since 1.2 |
86 |
|
*/ |
87 |
|
|
90 |
|
implements NavigableMap<K,V>, Cloneable, java.io.Serializable |
91 |
|
{ |
92 |
|
/** |
93 |
< |
* The Comparator used to maintain order in this TreeMap, or |
94 |
< |
* null if this TreeMap uses its elements natural ordering. |
93 |
> |
* The comparator used to maintain order in this tree map, or |
94 |
> |
* null if it uses the natural ordering of its keys. |
95 |
|
* |
96 |
|
* @serial |
97 |
|
*/ |
113 |
|
private void decrementSize() { modCount++; size--; } |
114 |
|
|
115 |
|
/** |
116 |
< |
* Constructs a new, empty map, sorted according to the keys' natural |
117 |
< |
* order. All keys inserted into the map must implement the |
118 |
< |
* <tt>Comparable</tt> interface. Furthermore, all such keys must be |
119 |
< |
* <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw a |
120 |
< |
* ClassCastException for any elements <tt>k1</tt> and <tt>k2</tt> in the |
121 |
< |
* map. If the user attempts to put a key into the map that violates this |
122 |
< |
* constraint (for example, the user attempts to put a string key into a |
123 |
< |
* map whose keys are integers), the <tt>put(Object key, Object |
124 |
< |
* value)</tt> call will throw a <tt>ClassCastException</tt>. |
125 |
< |
* |
126 |
< |
* @see Comparable |
116 |
> |
* Constructs a new, empty tree map, using the natural ordering of its |
117 |
> |
* keys. All keys inserted into the map must implement the {@link |
118 |
> |
* Comparable} interface. Furthermore, all such keys must be |
119 |
> |
* <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw |
120 |
> |
* a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and |
121 |
> |
* <tt>k2</tt> in the map. If the user attempts to put a key into the |
122 |
> |
* map that violates this constraint (for example, the user attempts to |
123 |
> |
* put a string key into a map whose keys are integers), the |
124 |
> |
* <tt>put(Object key, Object value)</tt> call will throw a |
125 |
> |
* <tt>ClassCastException</tt>. |
126 |
|
*/ |
127 |
|
public TreeMap() { |
128 |
|
} |
129 |
|
|
130 |
|
/** |
131 |
< |
* Constructs a new, empty map, sorted according to the given comparator. |
132 |
< |
* All keys inserted into the map must be <i>mutually comparable</i> by |
133 |
< |
* the given comparator: <tt>comparator.compare(k1, k2)</tt> must not |
134 |
< |
* throw a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and |
135 |
< |
* <tt>k2</tt> in the map. If the user attempts to put a key into the |
136 |
< |
* map that violates this constraint, the <tt>put(Object key, Object |
137 |
< |
* value)</tt> call will throw a <tt>ClassCastException</tt>. |
138 |
< |
* |
139 |
< |
* @param c the comparator that will be used to sort this map. A |
140 |
< |
* <tt>null</tt> value indicates that the keys' <i>natural |
141 |
< |
* ordering</i> should be used. |
142 |
< |
*/ |
143 |
< |
public TreeMap(Comparator<? super K> c) { |
144 |
< |
this.comparator = c; |
131 |
> |
* Constructs a new, empty tree map, ordered according to the given |
132 |
> |
* comparator. All keys inserted into the map must be <i>mutually |
133 |
> |
* comparable</i> by the given comparator: <tt>comparator.compare(k1, |
134 |
> |
* k2)</tt> must not throw a <tt>ClassCastException</tt> for any keys |
135 |
> |
* <tt>k1</tt> and <tt>k2</tt> in the map. If the user attempts to put |
136 |
> |
* a key into the map that violates this constraint, the <tt>put(Object |
137 |
> |
* key, Object value)</tt> call will throw a |
138 |
> |
* <tt>ClassCastException</tt>. |
139 |
> |
* |
140 |
> |
* @param comparator the comparator that will be used to order this map. |
141 |
> |
* If <tt>null</tt>, the {@linkplain Comparable natural |
142 |
> |
* ordering} of the keys will be used. |
143 |
> |
*/ |
144 |
> |
public TreeMap(Comparator<? super K> comparator) { |
145 |
> |
this.comparator = comparator; |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* Constructs a new map containing the same mappings as the given map, |
150 |
< |
* sorted according to the keys' <i>natural order</i>. All keys inserted |
151 |
< |
* into the new map must implement the <tt>Comparable</tt> interface. |
152 |
< |
* Furthermore, all such keys must be <i>mutually comparable</i>: |
153 |
< |
* <tt>k1.compareTo(k2)</tt> must not throw a <tt>ClassCastException</tt> |
154 |
< |
* for any elements <tt>k1</tt> and <tt>k2</tt> in the map. This method |
155 |
< |
* runs in n*log(n) time. |
156 |
< |
* |
157 |
< |
* @param m the map whose mappings are to be placed in this map. |
158 |
< |
* @throws ClassCastException the keys in t are not Comparable, or |
159 |
< |
* are not mutually comparable. |
160 |
< |
* @throws NullPointerException if the specified map is null. |
149 |
> |
* Constructs a new tree map containing the same mappings as the given |
150 |
> |
* map, ordered according to the <i>natural ordering</i> of its keys. |
151 |
> |
* All keys inserted into the new map must implement the {@link |
152 |
> |
* Comparable} interface. Furthermore, all such keys must be |
153 |
> |
* <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw |
154 |
> |
* a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and |
155 |
> |
* <tt>k2</tt> in the map. This method runs in n*log(n) time. |
156 |
> |
* |
157 |
> |
* @param m the map whose mappings are to be placed in this map |
158 |
> |
* @throws ClassCastException if the keys in m are not {@link Comparable}, |
159 |
> |
* or are not mutually comparable |
160 |
> |
* @throws NullPointerException if the specified map is null |
161 |
|
*/ |
162 |
|
public TreeMap(Map<? extends K, ? extends V> m) { |
163 |
|
putAll(m); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
< |
* Constructs a new map containing the same mappings as the given |
168 |
< |
* <tt>SortedMap</tt>, sorted according to the same ordering. This method |
169 |
< |
* runs in linear time. |
167 |
> |
* Constructs a new tree map containing the same mappings and |
168 |
> |
* using the same ordering as the specified sorted map. This |
169 |
> |
* method runs in linear time. |
170 |
|
* |
171 |
|
* @param m the sorted map whose mappings are to be placed in this map, |
172 |
< |
* and whose comparator is to be used to sort this map. |
173 |
< |
* @throws NullPointerException if the specified sorted map is null. |
172 |
> |
* and whose comparator is to be used to sort this map |
173 |
> |
* @throws NullPointerException if the specified map is null |
174 |
|
*/ |
175 |
|
public TreeMap(SortedMap<K, ? extends V> m) { |
176 |
|
comparator = m.comparator(); |
187 |
|
/** |
188 |
|
* Returns the number of key-value mappings in this map. |
189 |
|
* |
190 |
< |
* @return the number of key-value mappings in this map. |
190 |
> |
* @return the number of key-value mappings in this map |
191 |
|
*/ |
192 |
|
public int size() { |
193 |
|
return size; |
197 |
|
* Returns <tt>true</tt> if this map contains a mapping for the specified |
198 |
|
* key. |
199 |
|
* |
200 |
< |
* @param key key whose presence in this map is to be tested. |
201 |
< |
* |
200 |
> |
* @param key key whose presence in this map is to be tested |
201 |
|
* @return <tt>true</tt> if this map contains a mapping for the |
202 |
< |
* specified key. |
203 |
< |
* @throws ClassCastException if the key cannot be compared with the keys |
204 |
< |
* currently in the map. |
205 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
206 |
< |
* natural ordering, or its comparator does not tolerate |
207 |
< |
* <tt>null</tt> keys. |
202 |
> |
* specified key |
203 |
> |
* @throws ClassCastException if the specified key cannot be compared |
204 |
> |
* with the keys currently in the map |
205 |
> |
* @throws NullPointerException if the specified key is null |
206 |
> |
* and this map uses natural ordering, or its comparator |
207 |
> |
* does not permit null keys |
208 |
|
*/ |
209 |
|
public boolean containsKey(Object key) { |
210 |
|
return getEntry(key) != null; |
215 |
|
* specified value. More formally, returns <tt>true</tt> if and only if |
216 |
|
* this map contains at least one mapping to a value <tt>v</tt> such |
217 |
|
* that <tt>(value==null ? v==null : value.equals(v))</tt>. This |
218 |
< |
* operation will probably require time linear in the Map size for most |
219 |
< |
* implementations of Map. |
218 |
> |
* operation will probably require time linear in the map size for |
219 |
> |
* most implementations. |
220 |
|
* |
221 |
< |
* @param value value whose presence in this Map is to be tested. |
222 |
< |
* @return <tt>true</tt> if a mapping to <tt>value</tt> exists; |
223 |
< |
* <tt>false</tt> otherwise. |
221 |
> |
* @param value value whose presence in this map is to be tested |
222 |
> |
* @return <tt>true</tt> if a mapping to <tt>value</tt> exists; |
223 |
> |
* <tt>false</tt> otherwise |
224 |
|
* @since 1.2 |
225 |
|
*/ |
226 |
|
public boolean containsValue(Object value) { |
249 |
|
} |
250 |
|
|
251 |
|
/** |
252 |
< |
* Returns the value to which this map maps the specified key. Returns |
253 |
< |
* <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. |
252 |
> |
* Returns the value to which the specified key is mapped, |
253 |
> |
* or {@code null} if this map contains no mapping for the key. |
254 |
|
* |
255 |
< |
* @see #containsKey(Object) |
255 |
> |
* <p>More formally, if this map contains a mapping from a key |
256 |
> |
* {@code k} to a value {@code v} such that {@code key} compares |
257 |
> |
* equal to {@code k} according to the map's ordering, then this |
258 |
> |
* method returns {@code v}; otherwise it returns {@code null}. |
259 |
> |
* (There can be at most one such mapping.) |
260 |
> |
* |
261 |
> |
* <p>A return value of {@code null} does not <i>necessarily</i> |
262 |
> |
* indicate that the map contains no mapping for the key; it's also |
263 |
> |
* possible that the map explicitly maps the key to {@code null}. |
264 |
> |
* The {@link #containsKey containsKey} operation may be used to |
265 |
> |
* distinguish these two cases. |
266 |
> |
* |
267 |
> |
* @throws ClassCastException if the specified key cannot be compared |
268 |
> |
* with the keys currently in the map |
269 |
> |
* @throws NullPointerException if the specified key is null |
270 |
> |
* and this map uses natural ordering, or its comparator |
271 |
> |
* does not permit null keys |
272 |
|
*/ |
273 |
|
public V get(Object key) { |
274 |
|
Entry<K,V> p = getEntry(key); |
275 |
|
return (p==null ? null : p.value); |
276 |
|
} |
277 |
|
|
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 |
– |
*/ |
278 |
|
public Comparator<? super K> comparator() { |
279 |
|
return comparator; |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
< |
* 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. |
283 |
> |
* @throws NoSuchElementException {@inheritDoc} |
284 |
|
*/ |
285 |
|
public K firstKey() { |
286 |
|
return key(getFirstEntry()); |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
< |
* 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. |
290 |
> |
* @throws NoSuchElementException {@inheritDoc} |
291 |
|
*/ |
292 |
|
public K lastKey() { |
293 |
|
return key(getLastEntry()); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
297 |
< |
* Copies all of the mappings from the specified map to this map. These |
298 |
< |
* mappings replace any mappings that this map had for any of the keys |
299 |
< |
* currently in the specified map. |
300 |
< |
* |
301 |
< |
* @param map mappings to be stored in this map. |
302 |
< |
* @throws ClassCastException class of a key or value in the specified |
303 |
< |
* map prevents it from being stored in this map. |
304 |
< |
* |
305 |
< |
* @throws NullPointerException if the given map is <tt>null</tt> or |
306 |
< |
* this map does not permit <tt>null</tt> keys and a |
318 |
< |
* key in the specified map is <tt>null</tt>. |
297 |
> |
* Copies all of the mappings from the specified map to this map. |
298 |
> |
* These mappings replace any mappings that this map had for any |
299 |
> |
* of the keys currently in the specified map. |
300 |
> |
* |
301 |
> |
* @param map mappings to be stored in this map |
302 |
> |
* @throws ClassCastException if the class of a key or value in |
303 |
> |
* the specified map prevents it from being stored in this map |
304 |
> |
* @throws NullPointerException if the specified map is null or |
305 |
> |
* the specified map contains a null key and this map does not |
306 |
> |
* permit null keys |
307 |
|
*/ |
308 |
|
public void putAll(Map<? extends K, ? extends V> map) { |
309 |
|
int mapSize = map.size(); |
328 |
|
* does not contain an entry for the key. |
329 |
|
* |
330 |
|
* @return this map's entry for the given key, or <tt>null</tt> if the map |
331 |
< |
* does not contain an entry for the key. |
332 |
< |
* @throws ClassCastException if the key cannot be compared with the keys |
333 |
< |
* currently in the map. |
334 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
335 |
< |
* natural order, or its comparator does not tolerate * |
336 |
< |
* <tt>null</tt> keys. |
331 |
> |
* does not contain an entry for the key |
332 |
> |
* @throws ClassCastException if the specified key cannot be compared |
333 |
> |
* with the keys currently in the map |
334 |
> |
* @throws NullPointerException if the specified key is null |
335 |
> |
* and this map uses natural ordering, or its comparator |
336 |
> |
* does not permit null keys |
337 |
|
*/ |
338 |
|
private Entry<K,V> getEntry(Object key) { |
339 |
|
// Offload comparator-based version for sake of performance |
340 |
|
if (comparator != null) |
341 |
|
return getEntryUsingComparator(key); |
342 |
< |
Comparable<K> k = (Comparable<K>) key; |
342 |
> |
Comparable<? super K> k = (Comparable<? super K>) key; |
343 |
|
Entry<K,V> p = root; |
344 |
|
while (p != null) { |
345 |
|
int cmp = k.compareTo(p.key); |
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
< |
* Returns the key corresponding to the specified Entry. Throw |
517 |
< |
* NoSuchElementException if the Entry is <tt>null</tt>. |
516 |
> |
* Returns the key corresponding to the specified Entry. |
517 |
> |
* @throws NoSuchElementException if the Entry is null |
518 |
|
*/ |
519 |
|
private static <K> K key(Entry<K,?> e) { |
520 |
|
if (e==null) |
524 |
|
|
525 |
|
/** |
526 |
|
* Associates the specified value with the specified key in this map. |
527 |
< |
* If the map previously contained a mapping for this key, the old |
527 |
> |
* If the map previously contained a mapping for the key, the old |
528 |
|
* value is replaced. |
529 |
|
* |
530 |
< |
* @param key key with which the specified value is to be associated. |
531 |
< |
* @param value value to be associated with the specified key. |
530 |
> |
* @param key key with which the specified value is to be associated |
531 |
> |
* @param value value to be associated with the specified key |
532 |
|
* |
533 |
< |
* @return previous value associated with specified key, or <tt>null</tt> |
534 |
< |
* if there was no mapping for key. A <tt>null</tt> return can |
535 |
< |
* also indicate that the map previously associated <tt>null</tt> |
536 |
< |
* with the specified key. |
537 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
538 |
< |
* currently in the map. |
539 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
540 |
< |
* natural order, or its comparator does not tolerate |
541 |
< |
* <tt>null</tt> keys. |
533 |
> |
* @return the previous value associated with <tt>key</tt>, or |
534 |
> |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
535 |
> |
* (A <tt>null</tt> return can also indicate that the map |
536 |
> |
* previously associated <tt>null</tt> with <tt>key</tt>.) |
537 |
> |
* @throws ClassCastException if the specified key cannot be compared |
538 |
> |
* with the keys currently in the map |
539 |
> |
* @throws NullPointerException if the specified key is null |
540 |
> |
* and this map uses natural ordering, or its comparator |
541 |
> |
* does not permit null keys |
542 |
|
*/ |
543 |
|
public V put(K key, V value) { |
544 |
|
Entry<K,V> t = root; |
545 |
|
|
546 |
|
if (t == null) { |
547 |
+ |
// TBD |
548 |
+ |
// if (key == null) { |
549 |
+ |
// if (comparator == null) |
550 |
+ |
// throw new NullPointerException(); |
551 |
+ |
// comparator.compare(key, key); |
552 |
+ |
// } |
553 |
|
incrementSize(); |
554 |
|
root = new Entry<K,V>(key, value, null); |
555 |
|
return null; |
556 |
< |
} |
556 |
> |
} |
557 |
|
|
558 |
|
while (true) { |
559 |
|
int cmp = compare(key, t.key); |
585 |
|
* Removes the mapping for this key from this TreeMap if present. |
586 |
|
* |
587 |
|
* @param key key for which mapping should be removed |
588 |
< |
* @return previous value associated with specified key, or <tt>null</tt> |
589 |
< |
* if there was no mapping for key. A <tt>null</tt> return can |
590 |
< |
* also indicate that the map previously associated |
591 |
< |
* <tt>null</tt> with the specified key. |
592 |
< |
* |
593 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
594 |
< |
* currently in the map. |
595 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
596 |
< |
* natural order, or its comparator does not tolerate |
603 |
< |
* <tt>null</tt> keys. |
588 |
> |
* @return the previous value associated with <tt>key</tt>, or |
589 |
> |
* <tt>null</tt> if there was no mapping for <tt>key</tt>. |
590 |
> |
* (A <tt>null</tt> return can also indicate that the map |
591 |
> |
* previously associated <tt>null</tt> with <tt>key</tt>.) |
592 |
> |
* @throws ClassCastException if the specified key cannot be compared |
593 |
> |
* with the keys currently in the map |
594 |
> |
* @throws NullPointerException if the specified key is null |
595 |
> |
* and this map uses natural ordering, or its comparator |
596 |
> |
* does not permit null keys |
597 |
|
*/ |
598 |
|
public V remove(Object key) { |
599 |
|
Entry<K,V> p = getEntry(key); |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
< |
* Removes all mappings from this TreeMap. |
609 |
> |
* Removes all of the mappings from this map. |
610 |
> |
* The map will be empty after this call returns. |
611 |
|
*/ |
612 |
|
public void clear() { |
613 |
|
modCount++; |
619 |
|
* Returns a shallow copy of this <tt>TreeMap</tt> instance. (The keys and |
620 |
|
* values themselves are not cloned.) |
621 |
|
* |
622 |
< |
* @return a shallow copy of this Map. |
622 |
> |
* @return a shallow copy of this map |
623 |
|
*/ |
624 |
|
public Object clone() { |
625 |
|
TreeMap<K,V> clone = null; |
650 |
|
// NavigableMap API methods |
651 |
|
|
652 |
|
/** |
653 |
< |
* 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. |
653 |
> |
* @since 1.6 |
654 |
|
*/ |
655 |
|
public Map.Entry<K,V> firstEntry() { |
656 |
|
Entry<K,V> e = getFirstEntry(); |
657 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
657 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
< |
* Returns a key-value mapping associated with the greatest |
672 |
< |
* key in this map, or <tt>null</tt> if the map is empty. |
673 |
< |
* The returned entry does <em>not</em> support |
674 |
< |
* the <tt>Entry.setValue</tt> method. |
675 |
< |
* |
676 |
< |
* @return an Entry with greatest key, or <tt>null</tt> |
677 |
< |
* if the map is empty. |
661 |
> |
* @since 1.6 |
662 |
|
*/ |
663 |
|
public Map.Entry<K,V> lastEntry() { |
664 |
|
Entry<K,V> e = getLastEntry(); |
665 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
665 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
666 |
|
} |
667 |
|
|
668 |
|
/** |
669 |
< |
* Removes and returns a key-value mapping associated with |
686 |
< |
* the least key in this map, or <tt>null</tt> if the map is empty. |
687 |
< |
* |
688 |
< |
* @return the removed first entry of this map, or <tt>null</tt> |
689 |
< |
* if the map is empty. |
669 |
> |
* @since 1.6 |
670 |
|
*/ |
671 |
|
public Map.Entry<K,V> pollFirstEntry() { |
672 |
|
Entry<K,V> p = getFirstEntry(); |
673 |
< |
if (p == null) |
673 |
> |
if (p == null) |
674 |
|
return null; |
675 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(p); |
675 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(p); |
676 |
|
deleteEntry(p); |
677 |
|
return result; |
678 |
|
} |
679 |
|
|
680 |
|
/** |
681 |
< |
* Removes and returns a key-value mapping associated with |
702 |
< |
* the greatest key in this map, or <tt>null</tt> if the map is empty. |
703 |
< |
* |
704 |
< |
* @return the removed last entry of this map, or <tt>null</tt> |
705 |
< |
* if the map is empty. |
681 |
> |
* @since 1.6 |
682 |
|
*/ |
683 |
|
public Map.Entry<K,V> pollLastEntry() { |
684 |
|
Entry<K,V> p = getLastEntry(); |
685 |
< |
if (p == null) |
685 |
> |
if (p == null) |
686 |
|
return null; |
687 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(p); |
687 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(p); |
688 |
|
deleteEntry(p); |
689 |
|
return result; |
690 |
|
} |
691 |
|
|
692 |
|
/** |
693 |
< |
* Returns a key-value mapping associated with the least key |
694 |
< |
* greater than or equal to the given key, or <tt>null</tt> if |
695 |
< |
* there is no such entry. |
696 |
< |
* |
697 |
< |
* @param key the key. |
722 |
< |
* @return an Entry associated with ceiling of given key, or |
723 |
< |
* <tt>null</tt> if there is no such Entry. |
724 |
< |
* @throws ClassCastException if key cannot be compared with the |
725 |
< |
* keys currently in the map. |
726 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
727 |
< |
* natural order, or its comparator does not tolerate |
728 |
< |
* <tt>null</tt> keys. |
693 |
> |
* @throws ClassCastException {@inheritDoc} |
694 |
> |
* @throws NullPointerException if the specified key is null |
695 |
> |
* and this map uses natural ordering, or its comparator |
696 |
> |
* does not permit null keys |
697 |
> |
* @since 1.6 |
698 |
|
*/ |
699 |
< |
public Map.Entry<K,V> ceilingEntry(K key) { |
700 |
< |
Entry<K,V> e = getCeilingEntry(key); |
701 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
699 |
> |
public Map.Entry<K,V> lowerEntry(K key) { |
700 |
> |
Entry<K,V> e = getLowerEntry(key); |
701 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
702 |
|
} |
703 |
|
|
735 |
– |
|
704 |
|
/** |
705 |
< |
* Returns least key greater than or equal to the given key, or |
706 |
< |
* <tt>null</tt> if there is no such key. |
707 |
< |
* |
708 |
< |
* @param key the key. |
709 |
< |
* @return the ceiling key, or <tt>null</tt> |
742 |
< |
* if there is no such key. |
743 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
744 |
< |
* currently in the map. |
745 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
746 |
< |
* natural order, or its comparator does not tolerate |
747 |
< |
* <tt>null</tt> keys. |
705 |
> |
* @throws ClassCastException {@inheritDoc} |
706 |
> |
* @throws NullPointerException if the specified key is null |
707 |
> |
* and this map uses natural ordering, or its comparator |
708 |
> |
* does not permit null keys |
709 |
> |
* @since 1.6 |
710 |
|
*/ |
711 |
< |
public K ceilingKey(K key) { |
712 |
< |
Entry<K,V> e = getCeilingEntry(key); |
711 |
> |
public K lowerKey(K key) { |
712 |
> |
Entry<K,V> e = getLowerEntry(key); |
713 |
|
return (e == null)? null : e.key; |
714 |
|
} |
715 |
|
|
754 |
– |
|
755 |
– |
|
716 |
|
/** |
717 |
< |
* Returns a key-value mapping associated with the greatest key |
718 |
< |
* less than or equal to the given key, or <tt>null</tt> if there |
719 |
< |
* is no such entry. |
720 |
< |
* |
721 |
< |
* @param key the key. |
762 |
< |
* @return an Entry associated with floor of given key, or <tt>null</tt> |
763 |
< |
* if there is no such Entry. |
764 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
765 |
< |
* currently in the map. |
766 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
767 |
< |
* natural order, or its comparator does not tolerate |
768 |
< |
* <tt>null</tt> keys. |
717 |
> |
* @throws ClassCastException {@inheritDoc} |
718 |
> |
* @throws NullPointerException if the specified key is null |
719 |
> |
* and this map uses natural ordering, or its comparator |
720 |
> |
* does not permit null keys |
721 |
> |
* @since 1.6 |
722 |
|
*/ |
723 |
|
public Map.Entry<K,V> floorEntry(K key) { |
724 |
|
Entry<K,V> e = getFloorEntry(key); |
725 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
725 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
726 |
|
} |
727 |
|
|
728 |
|
/** |
729 |
< |
* Returns the greatest key |
730 |
< |
* less than or equal to the given key, or <tt>null</tt> if there |
731 |
< |
* is no such key. |
732 |
< |
* |
733 |
< |
* @param key the key. |
781 |
< |
* @return the floor of given key, or <tt>null</tt> if there is no |
782 |
< |
* such key. |
783 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
784 |
< |
* currently in the map. |
785 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
786 |
< |
* natural order, or its comparator does not tolerate |
787 |
< |
* <tt>null</tt> keys. |
729 |
> |
* @throws ClassCastException {@inheritDoc} |
730 |
> |
* @throws NullPointerException if the specified key is null |
731 |
> |
* and this map uses natural ordering, or its comparator |
732 |
> |
* does not permit null keys |
733 |
> |
* @since 1.6 |
734 |
|
*/ |
735 |
|
public K floorKey(K key) { |
736 |
|
Entry<K,V> e = getFloorEntry(key); |
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
< |
* Returns a key-value mapping associated with the least key |
742 |
< |
* strictly greater than the given key, or <tt>null</tt> if there |
743 |
< |
* is no such entry. |
744 |
< |
* |
745 |
< |
* @param key the key. |
800 |
< |
* @return an Entry with least key greater than the given key, or |
801 |
< |
* <tt>null</tt> if there is no such Entry. |
802 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
803 |
< |
* currently in the map. |
804 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
805 |
< |
* natural order, or its comparator does not tolerate |
806 |
< |
* <tt>null</tt> keys. |
741 |
> |
* @throws ClassCastException {@inheritDoc} |
742 |
> |
* @throws NullPointerException if the specified key is null |
743 |
> |
* and this map uses natural ordering, or its comparator |
744 |
> |
* does not permit null keys |
745 |
> |
* @since 1.6 |
746 |
|
*/ |
747 |
< |
public Map.Entry<K,V> higherEntry(K key) { |
748 |
< |
Entry<K,V> e = getHigherEntry(key); |
749 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
747 |
> |
public Map.Entry<K,V> ceilingEntry(K key) { |
748 |
> |
Entry<K,V> e = getCeilingEntry(key); |
749 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
750 |
|
} |
751 |
|
|
752 |
|
/** |
753 |
< |
* Returns the least key strictly greater than the given key, or |
754 |
< |
* <tt>null</tt> if there is no such key. |
755 |
< |
* |
756 |
< |
* @param key the key. |
757 |
< |
* @return the least key greater than the given key, or |
819 |
< |
* <tt>null</tt> if there is no such key. |
820 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
821 |
< |
* currently in the map. |
822 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
823 |
< |
* natural order, or its comparator does not tolerate |
824 |
< |
* <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 |
> |
* @since 1.6 |
758 |
|
*/ |
759 |
< |
public K higherKey(K key) { |
760 |
< |
Entry<K,V> e = getHigherEntry(key); |
759 |
> |
public K ceilingKey(K key) { |
760 |
> |
Entry<K,V> e = getCeilingEntry(key); |
761 |
|
return (e == null)? null : e.key; |
762 |
|
} |
763 |
|
|
764 |
|
/** |
765 |
< |
* Returns a key-value mapping associated with the greatest |
766 |
< |
* key strictly less than the given key, or <tt>null</tt> if there is no |
767 |
< |
* such entry. |
768 |
< |
* |
769 |
< |
* @param key the key. |
837 |
< |
* @return an Entry with greatest key less than the given |
838 |
< |
* key, or <tt>null</tt> if there is no such Entry. |
839 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
840 |
< |
* currently in the map. |
841 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
842 |
< |
* natural order, or its comparator does not tolerate |
843 |
< |
* <tt>null</tt> keys. |
765 |
> |
* @throws ClassCastException {@inheritDoc} |
766 |
> |
* @throws NullPointerException if the specified key is null |
767 |
> |
* and this map uses natural ordering, or its comparator |
768 |
> |
* does not permit null keys |
769 |
> |
* @since 1.6 |
770 |
|
*/ |
771 |
< |
public Map.Entry<K,V> lowerEntry(K key) { |
772 |
< |
Entry<K,V> e = getLowerEntry(key); |
773 |
< |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry(e); |
771 |
> |
public Map.Entry<K,V> higherEntry(K key) { |
772 |
> |
Entry<K,V> e = getHigherEntry(key); |
773 |
> |
return (e == null)? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
774 |
|
} |
775 |
|
|
776 |
|
/** |
777 |
< |
* Returns the greatest key strictly less than the given key, or |
778 |
< |
* <tt>null</tt> if there is no such key. |
779 |
< |
* |
780 |
< |
* @param key the key. |
781 |
< |
* @return the greatest key less than the given |
856 |
< |
* key, or <tt>null</tt> if there is no such key. |
857 |
< |
* @throws ClassCastException if key cannot be compared with the keys |
858 |
< |
* currently in the map. |
859 |
< |
* @throws NullPointerException if key is <tt>null</tt> and this map uses |
860 |
< |
* natural order, or its comparator does not tolerate |
861 |
< |
* <tt>null</tt> keys. |
777 |
> |
* @throws ClassCastException {@inheritDoc} |
778 |
> |
* @throws NullPointerException if the specified key is null |
779 |
> |
* and this map uses natural ordering, or its comparator |
780 |
> |
* does not permit null keys |
781 |
> |
* @since 1.6 |
782 |
|
*/ |
783 |
< |
public K lowerKey(K key) { |
784 |
< |
Entry<K,V> e = getLowerEntry(key); |
783 |
> |
public K higherKey(K key) { |
784 |
> |
Entry<K,V> e = getHigherEntry(key); |
785 |
|
return (e == null)? null : e.key; |
786 |
|
} |
787 |
|
|
794 |
|
*/ |
795 |
|
private transient Set<Map.Entry<K,V>> entrySet = null; |
796 |
|
private transient Set<Map.Entry<K,V>> descendingEntrySet = null; |
797 |
< |
private transient Set<K> descendingKeySet = null; |
878 |
< |
|
879 |
< |
transient Set<K> keySet = null; // XXX remove when integrated |
880 |
< |
transient Collection<V> values = null; // XXX remove when integrated |
797 |
> |
private transient Set<K> descendingKeySet = null; |
798 |
|
|
799 |
|
/** |
800 |
< |
* Returns a Set view of the keys contained in this map. The set's |
801 |
< |
* iterator will return the keys in ascending order. The set is backed by |
802 |
< |
* this <tt>TreeMap</tt> instance, so changes to this map are reflected in |
803 |
< |
* the Set, and vice-versa. The Set supports element removal, which |
804 |
< |
* removes the corresponding mapping from the map, via the |
805 |
< |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>, |
806 |
< |
* <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not support |
807 |
< |
* the <tt>add</tt> or <tt>addAll</tt> operations. |
808 |
< |
* |
809 |
< |
* @return a set view of the keys contained in this TreeMap. |
800 |
> |
* Returns a {@link Set} view of the keys contained in this map. |
801 |
> |
* The set's iterator returns the keys in ascending order. |
802 |
> |
* The set is backed by the map, so changes to the map are |
803 |
> |
* reflected in the set, and vice-versa. If the map is modified |
804 |
> |
* while an iteration over the set is in progress (except through |
805 |
> |
* the iterator's own <tt>remove</tt> operation), the results of |
806 |
> |
* the iteration are undefined. The set supports element removal, |
807 |
> |
* which removes the corresponding mapping from the map, via the |
808 |
> |
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
809 |
> |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
810 |
> |
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt> |
811 |
> |
* operations. |
812 |
|
*/ |
813 |
|
public Set<K> keySet() { |
814 |
|
Set<K> ks = keySet; |
819 |
|
public Iterator<K> iterator() { |
820 |
|
return new KeyIterator(getFirstEntry()); |
821 |
|
} |
822 |
< |
|
822 |
> |
|
823 |
|
public int size() { |
824 |
|
return TreeMap.this.size(); |
825 |
|
} |
826 |
< |
|
826 |
> |
|
827 |
|
public boolean contains(Object o) { |
828 |
|
return containsKey(o); |
829 |
|
} |
830 |
< |
|
830 |
> |
|
831 |
|
public boolean remove(Object o) { |
832 |
|
int oldSize = size; |
833 |
|
TreeMap.this.remove(o); |
834 |
|
return size != oldSize; |
835 |
|
} |
836 |
< |
|
836 |
> |
|
837 |
|
public void clear() { |
838 |
|
TreeMap.this.clear(); |
839 |
|
} |
840 |
|
} |
841 |
|
|
842 |
|
/** |
843 |
< |
* Returns a collection view of the values contained in this map. The |
844 |
< |
* collection's iterator will return the values in the order that their |
845 |
< |
* corresponding keys appear in the tree. The collection is backed by |
846 |
< |
* this <tt>TreeMap</tt> instance, so changes to this map are reflected in |
847 |
< |
* the collection, and vice-versa. The collection supports element |
848 |
< |
* removal, which removes the corresponding mapping from the map through |
849 |
< |
* the <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
850 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
851 |
< |
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
852 |
< |
* |
853 |
< |
* @return a collection view of the values contained in this map. |
843 |
> |
* Returns a {@link Collection} view of the values contained in this map. |
844 |
> |
* The collection's iterator returns the values in ascending order |
845 |
> |
* of the corresponding keys. |
846 |
> |
* The collection is backed by the map, so changes to the map are |
847 |
> |
* reflected in the collection, and vice-versa. If the map is |
848 |
> |
* modified while an iteration over the collection is in progress |
849 |
> |
* (except through the iterator's own <tt>remove</tt> operation), |
850 |
> |
* the results of the iteration are undefined. The collection |
851 |
> |
* supports element removal, which removes the corresponding |
852 |
> |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
853 |
> |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
854 |
> |
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not |
855 |
> |
* support the <tt>add</tt> or <tt>addAll</tt> operations. |
856 |
|
*/ |
857 |
|
public Collection<V> values() { |
858 |
|
Collection<V> vs = values; |
863 |
|
public Iterator<V> iterator() { |
864 |
|
return new ValueIterator(getFirstEntry()); |
865 |
|
} |
866 |
< |
|
866 |
> |
|
867 |
|
public int size() { |
868 |
|
return TreeMap.this.size(); |
869 |
|
} |
870 |
< |
|
870 |
> |
|
871 |
|
public boolean contains(Object o) { |
872 |
|
for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) |
873 |
|
if (valEquals(e.getValue(), o)) |
874 |
|
return true; |
875 |
|
return false; |
876 |
|
} |
877 |
< |
|
877 |
> |
|
878 |
|
public boolean remove(Object o) { |
879 |
|
for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) { |
880 |
|
if (valEquals(e.getValue(), o)) { |
884 |
|
} |
885 |
|
return false; |
886 |
|
} |
887 |
< |
|
887 |
> |
|
888 |
|
public void clear() { |
889 |
|
TreeMap.this.clear(); |
890 |
|
} |
891 |
|
} |
892 |
|
|
893 |
|
/** |
894 |
< |
* Returns a set view of the mappings contained in this map. The set's |
895 |
< |
* iterator returns the mappings in ascending key order. Each element in |
896 |
< |
* the returned set is a <tt>Map.Entry</tt>. The set is backed by this |
897 |
< |
* map, so changes to this map are reflected in the set, and vice-versa. |
898 |
< |
* The set supports element removal, which removes the corresponding |
899 |
< |
* mapping from the TreeMap, through the <tt>Iterator.remove</tt>, |
894 |
> |
* Returns a {@link Set} view of the mappings contained in this map. |
895 |
> |
* The set's iterator returns the entries in ascending key order. |
896 |
> |
* The set is backed by the map, so changes to the map are |
897 |
> |
* reflected in the set, and vice-versa. If the map is modified |
898 |
> |
* while an iteration over the set is in progress (except through |
899 |
> |
* the iterator's own <tt>remove</tt> operation, or through the |
900 |
> |
* <tt>setValue</tt> operation on a map entry returned by the |
901 |
> |
* iterator) the results of the iteration are undefined. The set |
902 |
> |
* supports element removal, which removes the corresponding |
903 |
> |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
904 |
|
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and |
905 |
< |
* <tt>clear</tt> operations. It does not support the <tt>add</tt> or |
906 |
< |
* <tt>addAll</tt> operations. |
982 |
< |
* |
983 |
< |
* @return a set view of the mappings contained in this map. |
984 |
< |
* @see Map.Entry |
905 |
> |
* <tt>clear</tt> operations. It does not support the |
906 |
> |
* <tt>add</tt> or <tt>addAll</tt> operations. |
907 |
|
*/ |
908 |
|
public Set<Map.Entry<K,V>> entrySet() { |
909 |
|
Set<Map.Entry<K,V>> es = entrySet; |
914 |
|
public Iterator<Map.Entry<K,V>> iterator() { |
915 |
|
return new EntryIterator(getFirstEntry()); |
916 |
|
} |
917 |
< |
|
917 |
> |
|
918 |
|
public boolean contains(Object o) { |
919 |
|
if (!(o instanceof Map.Entry)) |
920 |
|
return false; |
923 |
|
Entry<K,V> p = getEntry(entry.getKey()); |
924 |
|
return p != null && valEquals(p.getValue(), value); |
925 |
|
} |
926 |
< |
|
926 |
> |
|
927 |
|
public boolean remove(Object o) { |
928 |
|
if (!(o instanceof Map.Entry)) |
929 |
|
return false; |
936 |
|
} |
937 |
|
return false; |
938 |
|
} |
939 |
< |
|
939 |
> |
|
940 |
|
public int size() { |
941 |
|
return TreeMap.this.size(); |
942 |
|
} |
943 |
< |
|
943 |
> |
|
944 |
|
public void clear() { |
945 |
|
TreeMap.this.clear(); |
946 |
|
} |
947 |
|
} |
948 |
|
|
949 |
|
/** |
950 |
< |
* Returns a set view of the mappings contained in this map. The |
1029 |
< |
* set's iterator returns the mappings in descending key order. |
1030 |
< |
* Each element in the returned set is a <tt>Map.Entry</tt>. The |
1031 |
< |
* set is backed by this map, so changes to this map are reflected |
1032 |
< |
* in the set, and vice-versa. The set supports element removal, |
1033 |
< |
* which removes the corresponding mapping from the TreeMap, |
1034 |
< |
* through the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
1035 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> |
1036 |
< |
* operations. It does not support the <tt>add</tt> or |
1037 |
< |
* <tt>addAll</tt> operations. |
1038 |
< |
* |
1039 |
< |
* @return a set view of the mappings contained in this map, in |
1040 |
< |
* descending key order |
1041 |
< |
* @see Map.Entry |
950 |
> |
* @since 1.6 |
951 |
|
*/ |
952 |
|
public Set<Map.Entry<K,V>> descendingEntrySet() { |
953 |
|
Set<Map.Entry<K,V>> es = descendingEntrySet; |
961 |
|
} |
962 |
|
|
963 |
|
/** |
964 |
< |
* Returns a Set view of the keys contained in this map. The |
1056 |
< |
* set's iterator will return the keys in descending order. The |
1057 |
< |
* map is backed by this <tt>TreeMap</tt> instance, so changes to |
1058 |
< |
* this map are reflected in the Set, and vice-versa. The Set |
1059 |
< |
* supports element removal, which removes the corresponding |
1060 |
< |
* mapping from the map, via the <tt>Iterator.remove</tt>, |
1061 |
< |
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, |
1062 |
< |
* and <tt>clear</tt> operations. It does not support the |
1063 |
< |
* <tt>add</tt> or <tt>addAll</tt> operations. |
1064 |
< |
* |
1065 |
< |
* @return a set view of the keys contained in this TreeMap. |
964 |
> |
* @since 1.6 |
965 |
|
*/ |
966 |
|
public Set<K> descendingKeySet() { |
967 |
|
Set<K> ks = descendingKeySet; |
975 |
|
} |
976 |
|
|
977 |
|
/** |
978 |
< |
* Returns a view of the portion of this map whose keys range from |
1080 |
< |
* <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. (If |
1081 |
< |
* <tt>fromKey</tt> and <tt>toKey</tt> are equal, the returned |
1082 |
< |
* navigable map is empty.) The returned navigable map is backed |
1083 |
< |
* by this map, so changes in the returned navigable map are |
1084 |
< |
* reflected in this map, and vice-versa. The returned navigable |
1085 |
< |
* map supports all optional map operations.<p> |
1086 |
< |
* |
1087 |
< |
* The navigable map returned by this method will throw an |
1088 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1089 |
< |
* less than <tt>fromKey</tt> or greater than or equal to |
1090 |
< |
* <tt>toKey</tt>.<p> |
1091 |
< |
* |
1092 |
< |
* Note: this method always returns a <i>half-open range</i> (which |
1093 |
< |
* includes its low endpoint but not its high endpoint). If you need a |
1094 |
< |
* <i>closed range</i> (which includes both endpoints), and the key type |
1095 |
< |
* allows for calculation of the successor of a given key, merely request the |
1096 |
< |
* subrange from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>. |
1097 |
< |
* For example, suppose that <tt>m</tt> is a navigable map whose keys are |
1098 |
< |
* strings. The following idiom obtains a view containing all of the |
1099 |
< |
* key-value mappings in <tt>m</tt> whose keys are between <tt>low</tt> |
1100 |
< |
* and <tt>high</tt>, inclusive: |
1101 |
< |
* <pre> NavigableMap sub = m.navigableSubMap(low, high+"\0");</pre> |
1102 |
< |
* A similar technique can be used to generate an <i>open range</i> (which |
1103 |
< |
* contains neither endpoint). The following idiom obtains a view |
1104 |
< |
* containing all of the key-value mappings in <tt>m</tt> whose keys are |
1105 |
< |
* between <tt>low</tt> and <tt>high</tt>, exclusive: |
1106 |
< |
* <pre> NavigableMap sub = m.navigableSubMap(low+"\0", high);</pre> |
1107 |
< |
* |
1108 |
< |
* @param fromKey low endpoint (inclusive) of the subMap. |
1109 |
< |
* @param toKey high endpoint (exclusive) of the subMap. |
1110 |
< |
* |
1111 |
< |
* @return a view of the portion of this map whose keys range from |
1112 |
< |
* <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. |
1113 |
< |
* |
1114 |
< |
* @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt> |
1115 |
< |
* cannot be compared to one another using this map's comparator |
1116 |
< |
* (or, if the map has no comparator, using natural ordering). |
1117 |
< |
* @throws IllegalArgumentException if <tt>fromKey</tt> is greater than |
1118 |
< |
* <tt>toKey</tt>. |
978 |
> |
* @throws ClassCastException {@inheritDoc} |
979 |
|
* @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is |
980 |
< |
* <tt>null</tt> and this map uses natural order, or its |
981 |
< |
* comparator does not tolerate <tt>null</tt> keys. |
980 |
> |
* null and this map uses natural ordering, or its comparator |
981 |
> |
* does not permit null keys |
982 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
983 |
> |
* @since 1.6 |
984 |
|
*/ |
985 |
|
public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) { |
986 |
|
return new SubMap(fromKey, toKey); |
987 |
|
} |
988 |
|
|
1127 |
– |
|
989 |
|
/** |
990 |
< |
* Returns a view of the portion of this map whose keys are strictly less |
991 |
< |
* than <tt>toKey</tt>. The returned navigable map is backed by this map, so |
992 |
< |
* changes in the returned navigable map are reflected in this map, and |
993 |
< |
* vice-versa. The returned navigable map supports all optional map |
994 |
< |
* operations.<p> |
995 |
< |
* |
1135 |
< |
* The navigable map returned by this method will throw an |
1136 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1137 |
< |
* greater than or equal to <tt>toKey</tt>.<p> |
1138 |
< |
* |
1139 |
< |
* Note: this method always returns a view that does not contain its |
1140 |
< |
* (high) endpoint. If you need a view that does contain this endpoint, |
1141 |
< |
* and the key type allows for calculation of the successor of a given key, |
1142 |
< |
* merely request a headMap bounded by <tt>successor(highEndpoint)</tt>. |
1143 |
< |
* For example, suppose that suppose that <tt>m</tt> is a navigable map whose |
1144 |
< |
* keys are strings. The following idiom obtains a view containing all of |
1145 |
< |
* the key-value mappings in <tt>m</tt> whose keys are less than or equal |
1146 |
< |
* to <tt>high</tt>: |
1147 |
< |
* <pre> |
1148 |
< |
* NavigableMap head = m.headMap(high+"\0"); |
1149 |
< |
* </pre> |
1150 |
< |
* |
1151 |
< |
* @param toKey high endpoint (exclusive) of the headMap. |
1152 |
< |
* @return a view of the portion of this map whose keys are strictly |
1153 |
< |
* less than <tt>toKey</tt>. |
1154 |
< |
* |
1155 |
< |
* @throws ClassCastException if <tt>toKey</tt> is not compatible |
1156 |
< |
* with this map's comparator (or, if the map has no comparator, |
1157 |
< |
* if <tt>toKey</tt> does not implement <tt>Comparable</tt>). |
1158 |
< |
* @throws IllegalArgumentException if this map is itself a subMap, |
1159 |
< |
* headMap, or tailMap, and <tt>toKey</tt> is not within the |
1160 |
< |
* specified range of the subMap, headMap, or tailMap. |
1161 |
< |
* @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and |
1162 |
< |
* this map uses natural order, or its comparator does not |
1163 |
< |
* tolerate <tt>null</tt> keys. |
990 |
> |
* @throws ClassCastException {@inheritDoc} |
991 |
> |
* @throws NullPointerException if <tt>toKey</tt> is null |
992 |
> |
* and this map uses natural ordering, or its comparator |
993 |
> |
* does not permit null keys |
994 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
995 |
> |
* @since 1.6 |
996 |
|
*/ |
997 |
|
public NavigableMap<K,V> navigableHeadMap(K toKey) { |
998 |
|
return new SubMap(toKey, true); |
999 |
|
} |
1000 |
|
|
1001 |
|
/** |
1002 |
< |
* Returns a view of the portion of this map whose keys are greater than |
1003 |
< |
* or equal to <tt>fromKey</tt>. The returned navigable map is backed by |
1004 |
< |
* this map, so changes in the returned navigable map are reflected in this |
1005 |
< |
* map, and vice-versa. The returned navigable map supports all optional map |
1006 |
< |
* operations.<p> |
1007 |
< |
* |
1176 |
< |
* The navigable map returned by this method will throw an |
1177 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert a key |
1178 |
< |
* less than <tt>fromKey</tt>.<p> |
1179 |
< |
* |
1180 |
< |
* Note: this method always returns a view that contains its (low) |
1181 |
< |
* endpoint. If you need a view that does not contain this endpoint, and |
1182 |
< |
* the element type allows for calculation of the successor of a given value, |
1183 |
< |
* merely request a tailMap bounded by <tt>successor(lowEndpoint)</tt>. |
1184 |
< |
* For example, suppose that <tt>m</tt> is a navigable map whose keys |
1185 |
< |
* are strings. The following idiom obtains a view containing |
1186 |
< |
* all of the key-value mappings in <tt>m</tt> whose keys are strictly |
1187 |
< |
* greater than <tt>low</tt>: <pre> |
1188 |
< |
* NavigableMap tail = m.tailMap(low+"\0"); |
1189 |
< |
* </pre> |
1190 |
< |
* |
1191 |
< |
* @param fromKey low endpoint (inclusive) of the tailMap. |
1192 |
< |
* @return a view of the portion of this map whose keys are greater |
1193 |
< |
* than or equal to <tt>fromKey</tt>. |
1194 |
< |
* @throws ClassCastException if <tt>fromKey</tt> is not compatible |
1195 |
< |
* with this map's comparator (or, if the map has no comparator, |
1196 |
< |
* if <tt>fromKey</tt> does not implement <tt>Comparable</tt>). |
1197 |
< |
* @throws IllegalArgumentException if this map is itself a subMap, |
1198 |
< |
* headMap, or tailMap, and <tt>fromKey</tt> is not within the |
1199 |
< |
* specified range of the subMap, headMap, or tailMap. |
1200 |
< |
* @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and |
1201 |
< |
* this map uses natural order, or its comparator does not |
1202 |
< |
* tolerate <tt>null</tt> keys. |
1002 |
> |
* @throws ClassCastException {@inheritDoc} |
1003 |
> |
* @throws NullPointerException if <tt>fromKey</tt> is null |
1004 |
> |
* and this map uses natural ordering, or its comparator |
1005 |
> |
* does not permit null keys |
1006 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
1007 |
> |
* @since 1.6 |
1008 |
|
*/ |
1009 |
|
public NavigableMap<K,V> navigableTailMap(K fromKey) { |
1010 |
|
return new SubMap(fromKey, false); |
1011 |
|
} |
1012 |
|
|
1013 |
|
/** |
1014 |
< |
* Equivalent to <tt>navigableSubMap</tt> but with a return |
1015 |
< |
* type conforming to the <tt>SortedMap</tt> interface. |
1016 |
< |
* @param fromKey low endpoint (inclusive) of the subMap. |
1017 |
< |
* @param toKey high endpoint (exclusive) of the subMap. |
1018 |
< |
* |
1019 |
< |
* @return a view of the portion of this map whose keys range from |
1215 |
< |
* <tt>fromKey</tt>, inclusive, to <tt>toKey</tt>, exclusive. |
1216 |
< |
* |
1217 |
< |
* @throws ClassCastException if <tt>fromKey</tt> and <tt>toKey</tt> |
1218 |
< |
* cannot be compared to one another using this map's comparator |
1219 |
< |
* (or, if the map has no comparator, using natural ordering). |
1220 |
< |
* @throws IllegalArgumentException if <tt>fromKey</tt> is greater than |
1221 |
< |
* <tt>toKey</tt>. |
1014 |
> |
* Equivalent to {@link #navigableSubMap} but with a return type |
1015 |
> |
* conforming to the <tt>SortedMap</tt> interface. |
1016 |
> |
* |
1017 |
> |
* <p>{@inheritDoc} |
1018 |
> |
* |
1019 |
> |
* @throws ClassCastException {@inheritDoc} |
1020 |
|
* @throws NullPointerException if <tt>fromKey</tt> or <tt>toKey</tt> is |
1021 |
< |
* <tt>null</tt> and this map uses natural order, or its |
1022 |
< |
* comparator does not tolerate <tt>null</tt> keys. |
1021 |
> |
* null and this map uses natural ordering, or its comparator |
1022 |
> |
* does not permit null keys |
1023 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
1024 |
|
*/ |
1025 |
|
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
1026 |
|
return new SubMap(fromKey, toKey); |
1027 |
|
} |
1028 |
|
|
1230 |
– |
|
1029 |
|
/** |
1030 |
< |
* Equivalent to <tt>navigableHeadMap</tt> but with a return |
1031 |
< |
* type conforming to the <tt>SortedMap</tt> interface. |
1030 |
> |
* Equivalent to {@link #navigableHeadMap} but with a return type |
1031 |
> |
* conforming to the <tt>SortedMap</tt> interface. |
1032 |
> |
* |
1033 |
> |
* <p>{@inheritDoc} |
1034 |
|
* |
1035 |
< |
* @param toKey high endpoint (exclusive) of the headMap. |
1036 |
< |
* @return a view of the portion of this map whose keys are strictly |
1037 |
< |
* less than <tt>toKey</tt>. |
1038 |
< |
* |
1039 |
< |
* @throws ClassCastException if <tt>toKey</tt> is not compatible |
1240 |
< |
* with this map's comparator (or, if the map has no comparator, |
1241 |
< |
* if <tt>toKey</tt> does not implement <tt>Comparable</tt>). |
1242 |
< |
* @throws IllegalArgumentException if this map is itself a subMap, |
1243 |
< |
* headMap, or tailMap, and <tt>toKey</tt> is not within the |
1244 |
< |
* specified range of the subMap, headMap, or tailMap. |
1245 |
< |
* @throws NullPointerException if <tt>toKey</tt> is <tt>null</tt> and |
1246 |
< |
* this map uses natural order, or its comparator does not |
1247 |
< |
* tolerate <tt>null</tt> keys. |
1035 |
> |
* @throws ClassCastException {@inheritDoc} |
1036 |
> |
* @throws NullPointerException if <tt>toKey</tt> is null |
1037 |
> |
* and this map uses natural ordering, or its comparator |
1038 |
> |
* does not permit null keys |
1039 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
1040 |
|
*/ |
1041 |
|
public SortedMap<K,V> headMap(K toKey) { |
1042 |
|
return new SubMap(toKey, true); |
1043 |
|
} |
1044 |
|
|
1045 |
|
/** |
1046 |
< |
* Equivalent to <tt>navigableTailMap</tt> but with a return |
1047 |
< |
* type conforming to the <tt>SortedMap</tt> interface. |
1046 |
> |
* Equivalent to {@link #navigableTailMap} but with a return type |
1047 |
> |
* conforming to the <tt>SortedMap</tt> interface. |
1048 |
|
* |
1049 |
< |
* @param fromKey low endpoint (inclusive) of the tailMap. |
1050 |
< |
* @return a view of the portion of this map whose keys are greater |
1051 |
< |
* than or equal to <tt>fromKey</tt>. |
1052 |
< |
* @throws ClassCastException if <tt>fromKey</tt> is not compatible |
1053 |
< |
* with this map's comparator (or, if the map has no comparator, |
1054 |
< |
* if <tt>fromKey</tt> does not implement <tt>Comparable</tt>). |
1055 |
< |
* @throws IllegalArgumentException if this map is itself a subMap, |
1264 |
< |
* headMap, or tailMap, and <tt>fromKey</tt> is not within the |
1265 |
< |
* specified range of the subMap, headMap, or tailMap. |
1266 |
< |
* @throws NullPointerException if <tt>fromKey</tt> is <tt>null</tt> and |
1267 |
< |
* this map uses natural order, or its comparator does not |
1268 |
< |
* tolerate <tt>null</tt> keys. |
1049 |
> |
* <p>{@inheritDoc} |
1050 |
> |
* |
1051 |
> |
* @throws ClassCastException {@inheritDoc} |
1052 |
> |
* @throws NullPointerException if <tt>fromKey</tt> is null |
1053 |
> |
* and this map uses natural ordering, or its comparator |
1054 |
> |
* does not permit null keys |
1055 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
1056 |
|
*/ |
1057 |
|
public SortedMap<K,V> tailMap(K fromKey) { |
1058 |
|
return new SubMap(fromKey, false); |
1097 |
|
} |
1098 |
|
|
1099 |
|
public boolean isEmpty() { |
1100 |
< |
return entrySet.isEmpty(); |
1100 |
> |
return entrySet().isEmpty(); |
1101 |
|
} |
1102 |
|
|
1103 |
|
public boolean containsKey(Object key) { |
1104 |
< |
return inRange((K) key) && TreeMap.this.containsKey(key); |
1104 |
> |
return inRange(key) && TreeMap.this.containsKey(key); |
1105 |
|
} |
1106 |
|
|
1107 |
|
public V get(Object key) { |
1108 |
< |
if (!inRange((K) key)) |
1108 |
> |
if (!inRange(key)) |
1109 |
|
return null; |
1110 |
|
return TreeMap.this.get(key); |
1111 |
|
} |
1117 |
|
} |
1118 |
|
|
1119 |
|
public V remove(Object key) { |
1120 |
< |
if (!inRange((K) key)) |
1120 |
> |
if (!inRange(key)) |
1121 |
|
return null; |
1122 |
|
return TreeMap.this.remove(key); |
1123 |
|
} |
1130 |
|
TreeMap.Entry<K,V> e = fromStart ? getFirstEntry() : getCeilingEntry(fromKey); |
1131 |
|
K first = key(e); |
1132 |
|
if (!toEnd && compare(first, toKey) >= 0) |
1133 |
< |
throw(new NoSuchElementException()); |
1133 |
> |
throw new NoSuchElementException(); |
1134 |
|
return first; |
1135 |
|
} |
1136 |
|
|
1138 |
|
TreeMap.Entry<K,V> e = toEnd ? getLastEntry() : getLowerEntry(toKey); |
1139 |
|
K last = key(e); |
1140 |
|
if (!fromStart && compare(last, fromKey) < 0) |
1141 |
< |
throw(new NoSuchElementException()); |
1141 |
> |
throw new NoSuchElementException(); |
1142 |
|
return last; |
1143 |
|
} |
1144 |
|
|
1145 |
|
public Map.Entry<K,V> firstEntry() { |
1146 |
< |
TreeMap.Entry<K,V> e = fromStart ? |
1146 |
> |
TreeMap.Entry<K,V> e = fromStart ? |
1147 |
|
getFirstEntry() : getCeilingEntry(fromKey); |
1148 |
|
if (e == null || (!toEnd && compare(e.key, toKey) >= 0)) |
1149 |
|
return null; |
1151 |
|
} |
1152 |
|
|
1153 |
|
public Map.Entry<K,V> lastEntry() { |
1154 |
< |
TreeMap.Entry<K,V> e = toEnd ? |
1154 |
> |
TreeMap.Entry<K,V> e = toEnd ? |
1155 |
|
getLastEntry() : getLowerEntry(toKey); |
1156 |
|
if (e == null || (!fromStart && compare(e.key, fromKey) < 0)) |
1157 |
|
return null; |
1159 |
|
} |
1160 |
|
|
1161 |
|
public Map.Entry<K,V> pollFirstEntry() { |
1162 |
< |
TreeMap.Entry<K,V> e = fromStart ? |
1162 |
> |
TreeMap.Entry<K,V> e = fromStart ? |
1163 |
|
getFirstEntry() : getCeilingEntry(fromKey); |
1164 |
< |
if (e == null || (!fromStart && compare(e.key, fromKey) < 0)) |
1164 |
> |
if (e == null || (!toEnd && compare(e.key, toKey) >= 0)) |
1165 |
|
return null; |
1166 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(e); |
1166 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1167 |
|
deleteEntry(e); |
1168 |
|
return result; |
1169 |
|
} |
1170 |
|
|
1171 |
|
public Map.Entry<K,V> pollLastEntry() { |
1172 |
< |
TreeMap.Entry<K,V> e = toEnd ? |
1172 |
> |
TreeMap.Entry<K,V> e = toEnd ? |
1173 |
|
getLastEntry() : getLowerEntry(toKey); |
1174 |
< |
if (e == null || (!toEnd && compare(e.key, toKey) >= 0)) |
1174 |
> |
if (e == null || (!fromStart && compare(e.key, fromKey) < 0)) |
1175 |
|
return null; |
1176 |
< |
Map.Entry result = new AbstractMap.SimpleImmutableEntry(e); |
1176 |
> |
Map.Entry<K,V> result = new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1177 |
|
deleteEntry(e); |
1178 |
|
return result; |
1179 |
|
} |
1188 |
|
|
1189 |
|
public Map.Entry<K,V> ceilingEntry(K key) { |
1190 |
|
TreeMap.Entry<K,V> e = subceiling(key); |
1191 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1191 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1192 |
|
} |
1193 |
|
|
1194 |
|
public K ceilingKey(K key) { |
1207 |
|
|
1208 |
|
public Map.Entry<K,V> higherEntry(K key) { |
1209 |
|
TreeMap.Entry<K,V> e = subhigher(key); |
1210 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1210 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1211 |
|
} |
1212 |
|
|
1213 |
|
public K higherKey(K key) { |
1225 |
|
|
1226 |
|
public Map.Entry<K,V> floorEntry(K key) { |
1227 |
|
TreeMap.Entry<K,V> e = subfloor(key); |
1228 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1228 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1229 |
|
} |
1230 |
|
|
1231 |
|
public K floorKey(K key) { |
1243 |
|
|
1244 |
|
public Map.Entry<K,V> lowerEntry(K key) { |
1245 |
|
TreeMap.Entry<K,V> e = sublower(key); |
1246 |
< |
return e == null? null : new AbstractMap.SimpleImmutableEntry(e); |
1246 |
> |
return e == null? null : new AbstractMap.SimpleImmutableEntry<K,V>(e); |
1247 |
|
} |
1248 |
|
|
1249 |
|
public K lowerKey(K key) { |
1251 |
|
return e == null? null : e.key; |
1252 |
|
} |
1253 |
|
|
1254 |
< |
private transient Set<Map.Entry<K,V>> entrySet = new EntrySetView(); |
1254 |
> |
private transient Set<Map.Entry<K,V>> entrySet = null; |
1255 |
|
|
1256 |
|
public Set<Map.Entry<K,V>> entrySet() { |
1257 |
< |
return entrySet; |
1257 |
> |
Set<Map.Entry<K,V>> es = entrySet; |
1258 |
> |
return (es != null)? es : (entrySet = new EntrySetView()); |
1259 |
|
} |
1260 |
|
|
1261 |
|
private class EntrySetView extends AbstractSet<Map.Entry<K,V>> { |
1312 |
|
} |
1313 |
|
|
1314 |
|
private transient Set<Map.Entry<K,V>> descendingEntrySetView = null; |
1315 |
< |
private transient Set<K> descendingKeySetView = null; |
1315 |
> |
private transient Set<K> descendingKeySetView = null; |
1316 |
|
|
1317 |
|
public Set<Map.Entry<K,V>> descendingEntrySet() { |
1318 |
|
Set<Map.Entry<K,V>> es = descendingEntrySetView; |
1319 |
< |
return (es != null) ? es : (descendingEntrySetView = new DescendingEntrySetView()); |
1319 |
> |
return (es != null) ? es : |
1320 |
> |
(descendingEntrySetView = new DescendingEntrySetView()); |
1321 |
|
} |
1322 |
|
|
1323 |
|
public Set<K> descendingKeySet() { |
1324 |
|
Set<K> ks = descendingKeySetView; |
1325 |
< |
return (ks != null) ? ks : (descendingKeySetView = new DescendingKeySetView()); |
1325 |
> |
return (ks != null) ? ks : |
1326 |
> |
(descendingKeySetView = new DescendingKeySetView()); |
1327 |
|
} |
1328 |
|
|
1329 |
|
private class DescendingEntrySetView extends EntrySetView { |
1338 |
|
public Iterator<K> iterator() { |
1339 |
|
return new Iterator<K>() { |
1340 |
|
private Iterator<Entry<K,V>> i = descendingEntrySet().iterator(); |
1341 |
< |
|
1341 |
> |
|
1342 |
|
public boolean hasNext() { return i.hasNext(); } |
1343 |
|
public K next() { return i.next().getKey(); } |
1344 |
|
public void remove() { i.remove(); } |
1345 |
|
}; |
1346 |
|
} |
1347 |
< |
|
1347 |
> |
|
1348 |
|
public int size() { |
1349 |
|
return SubMap.this.size(); |
1350 |
|
} |
1351 |
< |
|
1351 |
> |
|
1352 |
|
public boolean contains(Object k) { |
1353 |
|
return SubMap.this.containsKey(k); |
1354 |
|
} |
1355 |
|
} |
1356 |
|
|
1567 |
– |
|
1357 |
|
public NavigableMap<K,V> navigableSubMap(K fromKey, K toKey) { |
1358 |
|
if (!inRange2(fromKey)) |
1359 |
|
throw new IllegalArgumentException("fromKey out of range"); |
1374 |
|
return new SubMap(false, fromKey, toEnd, toKey); |
1375 |
|
} |
1376 |
|
|
1588 |
– |
|
1377 |
|
public SortedMap<K,V> subMap(K fromKey, K toKey) { |
1378 |
|
return navigableSubMap(fromKey, toKey); |
1379 |
|
} |
1386 |
|
return navigableTailMap(fromKey); |
1387 |
|
} |
1388 |
|
|
1389 |
< |
private boolean inRange(K key) { |
1389 |
> |
private boolean inRange(Object key) { |
1390 |
|
return (fromStart || compare(key, fromKey) >= 0) && |
1391 |
|
(toEnd || compare(key, toKey) < 0); |
1392 |
|
} |
1393 |
|
|
1394 |
|
// This form allows the high endpoint (as well as all legit keys) |
1395 |
< |
private boolean inRange2(K key) { |
1395 |
> |
private boolean inRange2(Object key) { |
1396 |
|
return (fromStart || compare(key, fromKey) >= 0) && |
1397 |
|
(toEnd || compare(key, toKey) <= 0); |
1398 |
|
} |
1414 |
|
return next != null; |
1415 |
|
} |
1416 |
|
|
1417 |
< |
Entry<K,V> nextEntry() { |
1417 |
> |
Entry<K,V> nextEntry() { |
1418 |
|
if (next == null) |
1419 |
|
throw new NoSuchElementException(); |
1420 |
|
if (modCount != expectedModCount) |
1441 |
|
EntryIterator(Entry<K,V> first) { |
1442 |
|
super(first); |
1443 |
|
} |
1656 |
– |
|
1444 |
|
public Map.Entry<K,V> next() { |
1445 |
|
return nextEntry(); |
1446 |
|
} |
1485 |
|
} |
1486 |
|
} |
1487 |
|
|
1701 |
– |
|
1488 |
|
/** |
1489 |
|
* Base for Descending Iterators. |
1490 |
|
*/ |
1545 |
|
|
1546 |
|
} |
1547 |
|
|
1762 |
– |
|
1548 |
|
/** |
1549 |
|
* Compares two keys using the correct comparison method for this TreeMap. |
1550 |
|
*/ |
1551 |
< |
private int compare(K k1, K k2) { |
1552 |
< |
return (comparator==null ? ((Comparable</*-*/K>)k1).compareTo(k2) |
1553 |
< |
: comparator.compare((K)k1, (K)k2)); |
1551 |
> |
private int compare(Object k1, Object k2) { |
1552 |
> |
return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2) |
1553 |
> |
: comparator.compare((K)k1, (K)k2); |
1554 |
|
} |
1555 |
|
|
1556 |
|
/** |
1557 |
< |
* Test two values for equality. Differs from o1.equals(o2) only in |
1557 |
> |
* Test two values for equality. Differs from o1.equals(o2) only in |
1558 |
|
* that it copes with <tt>null</tt> o1 properly. |
1559 |
|
*/ |
1560 |
|
private static boolean valEquals(Object o1, Object o2) { |
1590 |
|
/** |
1591 |
|
* Returns the key. |
1592 |
|
* |
1593 |
< |
* @return the key. |
1593 |
> |
* @return the key |
1594 |
|
*/ |
1595 |
|
public K getKey() { |
1596 |
|
return key; |
1599 |
|
/** |
1600 |
|
* Returns the value associated with the key. |
1601 |
|
* |
1602 |
< |
* @return the value associated with the key. |
1602 |
> |
* @return the value associated with the key |
1603 |
|
*/ |
1604 |
|
public V getValue() { |
1605 |
|
return value; |
1610 |
|
* value. |
1611 |
|
* |
1612 |
|
* @return the value associated with the key before this method was |
1613 |
< |
* called. |
1613 |
> |
* called |
1614 |
|
*/ |
1615 |
|
public V setValue(V value) { |
1616 |
|
V oldValue = this.value; |
1981 |
|
} |
1982 |
|
|
1983 |
|
/** Intended to be called only from TreeSet.addAll **/ |
1984 |
< |
void addAllForTreeSet(SortedSet<Map.Entry<K,V>> set, V defaultVal) { |
1984 |
> |
void addAllForTreeSet(SortedSet<? extends K> set, V defaultVal) { |
1985 |
|
try { |
1986 |
|
buildFromSorted(set.size(), set.iterator(), null, defaultVal); |
1987 |
|
} catch (java.io.IOException cannotHappen) { |
2006 |
|
* to calling this method. |
2007 |
|
* |
2008 |
|
* @param size the number of keys (or key-value pairs) to be read from |
2009 |
< |
* the iterator or stream. |
2009 |
> |
* the iterator or stream |
2010 |
|
* @param it If non-null, new entries are created from entries |
2011 |
|
* or keys read from this iterator. |
2012 |
|
* @param str If non-null, new entries are created from keys and |
2020 |
|
* @throws ClassNotFoundException propagated from readObject. |
2021 |
|
* This cannot occur if str is null. |
2022 |
|
*/ |
2023 |
< |
private |
2024 |
< |
void buildFromSorted(int size, Iterator it, |
2025 |
< |
java.io.ObjectInputStream str, |
2241 |
< |
V defaultVal) |
2023 |
> |
private void buildFromSorted(int size, Iterator it, |
2024 |
> |
java.io.ObjectInputStream str, |
2025 |
> |
V defaultVal) |
2026 |
|
throws java.io.IOException, ClassNotFoundException { |
2027 |
|
this.size = size; |
2028 |
< |
root = |
2029 |
< |
buildFromSorted(0, 0, size-1, computeRedLevel(size), |
2246 |
< |
it, str, defaultVal); |
2028 |
> |
root = buildFromSorted(0, 0, size-1, computeRedLevel(size), |
2029 |
> |
it, str, defaultVal); |
2030 |
|
} |
2031 |
|
|
2032 |
|
/** |
2033 |
|
* Recursive "helper method" that does the real work of the |
2034 |
< |
* of the previous method. Identically named parameters have |
2034 |
> |
* previous method. Identically named parameters have |
2035 |
|
* identical definitions. Additional parameters are documented below. |
2036 |
|
* It is assumed that the comparator and size fields of the TreeMap are |
2037 |
|
* already set prior to calling this method. (It ignores both fields.) |
2039 |
|
* @param level the current level of tree. Initial call should be 0. |
2040 |
|
* @param lo the first element index of this subtree. Initial should be 0. |
2041 |
|
* @param hi the last element index of this subtree. Initial should be |
2042 |
< |
* size-1. |
2042 |
> |
* size-1. |
2043 |
|
* @param redLevel the level at which nodes should be red. |
2044 |
|
* Must be equal to computeRedLevel for tree of this size. |
2045 |
|
*/ |
2123 |
|
level++; |
2124 |
|
return level; |
2125 |
|
} |
2343 |
– |
|
2126 |
|
} |