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; |
8 |
> |
package java.util; |
9 |
|
|
10 |
|
/** |
11 |
< |
* This class implements the <tt>Set</tt> interface, backed by a |
12 |
< |
* <tt>TreeMap</tt> instance. This class guarantees that the sorted set will |
13 |
< |
* be in ascending element order, sorted according to the <i>natural order</i> |
14 |
< |
* of the elements (see <tt>Comparable</tt>), or by the comparator provided at |
15 |
< |
* set creation time, depending on which constructor is used.<p> |
11 |
> |
* A {@link NavigableSet} implementation based on a {@link TreeMap}. |
12 |
> |
* The elements are ordered using their {@linkplain Comparable natural |
13 |
> |
* ordering}, or by a {@link Comparator} provided at set creation |
14 |
> |
* time, depending on which constructor is used. |
15 |
|
* |
16 |
< |
* This implementation provides guaranteed log(n) time cost for the basic |
17 |
< |
* operations (<tt>add</tt>, <tt>remove</tt> and <tt>contains</tt>).<p> |
16 |
> |
* <p>This implementation provides guaranteed log(n) time cost for the basic |
17 |
> |
* operations (<tt>add</tt>, <tt>remove</tt> and <tt>contains</tt>). |
18 |
|
* |
19 |
< |
* Note that the ordering maintained by a set (whether or not an explicit |
19 |
> |
* <p>Note that the ordering maintained by a set (whether or not an explicit |
20 |
|
* comparator is provided) must be <i>consistent with equals</i> if it is to |
21 |
|
* correctly implement the <tt>Set</tt> interface. (See <tt>Comparable</tt> |
22 |
|
* or <tt>Comparator</tt> for a precise definition of <i>consistent with |
23 |
|
* equals</i>.) This is so because the <tt>Set</tt> interface is defined in |
24 |
|
* terms of the <tt>equals</tt> operation, but a <tt>TreeSet</tt> instance |
25 |
< |
* performs all key comparisons using its <tt>compareTo</tt> (or |
26 |
< |
* <tt>compare</tt>) method, so two keys that are deemed equal by this method |
25 |
> |
* performs all element comparisons using its <tt>compareTo</tt> (or |
26 |
> |
* <tt>compare</tt>) method, so two elements that are deemed equal by this method |
27 |
|
* are, from the standpoint of the set, equal. The behavior of a set |
28 |
|
* <i>is</i> well-defined even if its ordering is inconsistent with equals; it |
29 |
< |
* just fails to obey the general contract of the <tt>Set</tt> interface.<p> |
29 |
> |
* just fails to obey the general contract of the <tt>Set</tt> interface. |
30 |
|
* |
31 |
< |
* <b>Note that this implementation is not synchronized.</b> If multiple |
31 |
> |
* <p><b>Note that this implementation is not synchronized.</b> If multiple |
32 |
|
* threads access a set concurrently, and at least one of the threads modifies |
33 |
|
* the set, it <i>must</i> be synchronized externally. This is typically |
34 |
|
* accomplished by synchronizing on some object that naturally encapsulates |
36 |
|
* <tt>Collections.synchronizedSet</tt> method. This is best done at creation |
37 |
|
* time, to prevent accidental unsynchronized access to the set: <pre> |
38 |
|
* SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...)); |
39 |
< |
* </pre><p> |
39 |
> |
* </pre> |
40 |
|
* |
41 |
< |
* The Iterators returned by this class's <tt>iterator</tt> method are |
41 |
> |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
42 |
|
* <i>fail-fast</i>: if the set is modified at any time after the iterator is |
43 |
|
* created, in any way except through the iterator's own <tt>remove</tt> |
44 |
< |
* method, the iterator will throw a <tt>ConcurrentModificationException</tt>. |
44 |
> |
* method, the iterator will throw a {@link ConcurrentModificationException}. |
45 |
|
* Thus, in the face of concurrent modification, the iterator fails quickly |
46 |
|
* and cleanly, rather than risking arbitrary, non-deterministic behavior at |
47 |
|
* an undetermined time in the future. |
52 |
|
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
53 |
|
* Therefore, it would be wrong to write a program that depended on this |
54 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
55 |
< |
* should be used only to detect bugs.</i><p> |
55 |
> |
* should be used only to detect bugs.</i> |
56 |
|
* |
57 |
< |
* This class is a member of the |
57 |
> |
* <p>This class is a member of the |
58 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
59 |
|
* Java Collections Framework</a>. |
60 |
|
* |
61 |
+ |
* @param <E> the type of elements maintained by this set |
62 |
+ |
* |
63 |
|
* @author Josh Bloch |
64 |
|
* @version %I%, %G% |
65 |
|
* @see Collection |
82 |
|
private static final Object PRESENT = new Object(); |
83 |
|
|
84 |
|
/** |
85 |
< |
* Constructs a set backed by the specified sorted map. |
85 |
> |
* Constructs a set backed by the specified navigable map. |
86 |
|
*/ |
87 |
|
private TreeSet(NavigableMap<E,Object> m) { |
88 |
|
this.m = m; |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* Constructs a new, empty set, sorted according to the elements' natural |
93 |
< |
* order. All elements inserted into the set must implement the |
94 |
< |
* <tt>Comparable</tt> interface. Furthermore, all such elements must be |
95 |
< |
* <i>mutually comparable</i>: <tt>e1.compareTo(e2)</tt> must not throw a |
92 |
> |
* Constructs a new, empty tree set, sorted according to the |
93 |
> |
* natural ordering of its elements. All elements inserted into |
94 |
> |
* the set must implement the {@link Comparable} interface. |
95 |
> |
* Furthermore, all such elements must be <i>mutually |
96 |
> |
* comparable</i>: <tt>e1.compareTo(e2)</tt> must not throw a |
97 |
|
* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and |
98 |
< |
* <tt>e2</tt> in the set. If the user attempts to add an element to the |
99 |
< |
* set that violates this constraint (for example, the user attempts to |
100 |
< |
* add a string element to a set whose elements are integers), the |
101 |
< |
* <tt>add(Object)</tt> call will throw a <tt>ClassCastException</tt>. |
102 |
< |
* |
101 |
< |
* @see Comparable |
98 |
> |
* <tt>e2</tt> in the set. If the user attempts to add an element |
99 |
> |
* to the set that violates this constraint (for example, the user |
100 |
> |
* attempts to add a string element to a set whose elements are |
101 |
> |
* integers), the <tt>add(Object)</tt> call will throw a |
102 |
> |
* <tt>ClassCastException</tt>. |
103 |
|
*/ |
104 |
|
public TreeSet() { |
105 |
|
this(new TreeMap<E,Object>()); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* Constructs a new, empty set, sorted according to the specified |
110 |
< |
* comparator. All elements inserted into the set must be <i>mutually |
111 |
< |
* comparable</i> by the specified comparator: <tt>comparator.compare(e1, |
112 |
< |
* e2)</tt> must not throw a <tt>ClassCastException</tt> for any elements |
113 |
< |
* <tt>e1</tt> and <tt>e2</tt> in the set. If the user attempts to add |
114 |
< |
* an element to the set that violates this constraint, the |
115 |
< |
* <tt>add(Object)</tt> call will throw a <tt>ClassCastException</tt>. |
116 |
< |
* |
117 |
< |
* @param c the comparator that will be used to sort this set. A |
118 |
< |
* <tt>null</tt> value indicates that the elements' <i>natural |
119 |
< |
* ordering</i> should be used. |
120 |
< |
*/ |
121 |
< |
public TreeSet(Comparator<? super E> c) { |
122 |
< |
this(new TreeMap<E,Object>(c)); |
109 |
> |
* Constructs a new, empty tree set, sorted according to the |
110 |
> |
* specified comparator. All elements inserted into the set must |
111 |
> |
* be <i>mutually comparable</i> by the specified comparator: |
112 |
> |
* <tt>comparator.compare(e1, e2)</tt> must not throw a |
113 |
> |
* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and |
114 |
> |
* <tt>e2</tt> in the set. If the user attempts to add an element |
115 |
> |
* to the set that violates this constraint, the |
116 |
> |
* <tt>add(Object)</tt> call will throw a |
117 |
> |
* <tt>ClassCastException</tt>. |
118 |
> |
* |
119 |
> |
* @param comparator the comparator that will be used to order this set. |
120 |
> |
* If <tt>null</tt>, the {@linkplain Comparable natural |
121 |
> |
* ordering} of the elements will be used. |
122 |
> |
*/ |
123 |
> |
public TreeSet(Comparator<? super E> comparator) { |
124 |
> |
this(new TreeMap<E,Object>(comparator)); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* Constructs a new set containing the elements in the specified |
129 |
< |
* collection, sorted according to the elements' <i>natural order</i>. |
130 |
< |
* All keys inserted into the set must implement the <tt>Comparable</tt> |
131 |
< |
* interface. Furthermore, all such keys must be <i>mutually |
132 |
< |
* comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw a |
133 |
< |
* <tt>ClassCastException</tt> for any elements <tt>k1</tt> and |
134 |
< |
* <tt>k2</tt> in the set. |
135 |
< |
* |
136 |
< |
* @param c The elements that will comprise the new set. |
137 |
< |
* |
138 |
< |
* @throws ClassCastException if the keys in the specified collection are |
139 |
< |
* not comparable, or are not mutually comparable. |
140 |
< |
* @throws NullPointerException if the specified collection is null. |
128 |
> |
* Constructs a new tree set containing the elements in the |
129 |
> |
* specified collection, sorted according to the <i>natural |
130 |
> |
* ordering</i> of its elements. All elements inserted into the |
131 |
> |
* set must implement the {@link Comparable} interface. |
132 |
> |
* Furthermore, all such elements must be <i>mutually |
133 |
> |
* comparable</i>: <tt>e1.compareTo(e2)</tt> must not throw a |
134 |
> |
* <tt>ClassCastException</tt> for any elements <tt>e1</tt> and |
135 |
> |
* <tt>e2</tt> in the set. |
136 |
> |
* |
137 |
> |
* @param c collection whose elements will comprise the new set |
138 |
> |
* @throws ClassCastException if the elements in <tt>c</tt> are |
139 |
> |
* not {@link Comparable}, or are not mutually comparable |
140 |
> |
* @throws NullPointerException if the specified collection is null |
141 |
|
*/ |
142 |
|
public TreeSet(Collection<? extends E> c) { |
143 |
|
this(); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* Constructs a new set containing the same elements as the specified |
149 |
< |
* sorted set, sorted according to the same ordering. |
148 |
> |
* Constructs a new tree set containing the same elements and |
149 |
> |
* using the same ordering as the specified sorted set. |
150 |
|
* |
151 |
< |
* @param s sorted set whose elements will comprise the new set. |
152 |
< |
* @throws NullPointerException if the specified sorted set is null. |
151 |
> |
* @param s sorted set whose elements will comprise the new set |
152 |
> |
* @throws NullPointerException if the specified sorted set is null |
153 |
|
*/ |
154 |
|
public TreeSet(SortedSet<E> s) { |
155 |
|
this(s.comparator()); |
160 |
|
* Returns an iterator over the elements in this set. The elements |
161 |
|
* are returned in ascending order. |
162 |
|
* |
163 |
< |
* @return an iterator over the elements in this set. |
163 |
> |
* @return an iterator over the elements in this set |
164 |
|
*/ |
165 |
|
public Iterator<E> iterator() { |
166 |
< |
return m.keySet().iterator(); |
166 |
> |
return m.keySet().iterator(); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
170 |
|
* Returns an iterator over the elements in this set. The elements |
171 |
|
* are returned in descending order. |
172 |
|
* |
173 |
< |
* @return an iterator over the elements in this set. |
173 |
> |
* @return an iterator over the elements in this set |
174 |
|
*/ |
175 |
|
public Iterator<E> descendingIterator() { |
176 |
|
return m.descendingKeySet().iterator(); |
179 |
|
/** |
180 |
|
* Returns the number of elements in this set (its cardinality). |
181 |
|
* |
182 |
< |
* @return the number of elements in this set (its cardinality). |
182 |
> |
* @return the number of elements in this set (its cardinality) |
183 |
|
*/ |
184 |
|
public int size() { |
185 |
|
return m.size(); |
188 |
|
/** |
189 |
|
* Returns <tt>true</tt> if this set contains no elements. |
190 |
|
* |
191 |
< |
* @return <tt>true</tt> if this set contains no elements. |
191 |
> |
* @return <tt>true</tt> if this set contains no elements |
192 |
|
*/ |
193 |
|
public boolean isEmpty() { |
194 |
|
return m.isEmpty(); |
197 |
|
/** |
198 |
|
* Returns <tt>true</tt> if this set contains the specified element. |
199 |
|
* |
200 |
< |
* @param o the object to be checked for containment in this set. |
201 |
< |
* @return <tt>true</tt> if this set contains the specified element. |
199 |
< |
* |
200 |
> |
* @param o the object to be checked for containment in this set |
201 |
> |
* @return <tt>true</tt> if this set contains the specified element |
202 |
|
* @throws ClassCastException if the specified object cannot be compared |
203 |
< |
* with the elements currently in the set. |
204 |
< |
* @throws NullPointerException o is <tt>null</tt> and this map |
205 |
< |
* uses natural ordering and is non-empty, or its comparator does |
206 |
< |
* not tolerate <tt>null</tt> keys. |
203 |
> |
* with the elements currently in the set |
204 |
> |
* @throws NullPointerException if the specified element is null and |
205 |
> |
* this set uses natural ordering and is non-empty, or its |
206 |
> |
* comparator does not permit null elements |
207 |
|
*/ |
208 |
|
public boolean contains(Object o) { |
209 |
|
return m.containsKey(o); |
212 |
|
/** |
213 |
|
* Adds the specified element to this set if it is not already present. |
214 |
|
* |
215 |
< |
* @param o element to be added to this set. |
215 |
> |
* @param e element to be added to this set |
216 |
|
* @return <tt>true</tt> if the set did not already contain the specified |
217 |
< |
* element. |
216 |
< |
* |
217 |
> |
* element |
218 |
|
* @throws ClassCastException if the specified object cannot be compared |
219 |
< |
* with the elements currently in the set. |
220 |
< |
* @throws NullPointerException o is <tt>null</tt> and this map |
221 |
< |
* uses natural ordering and is non-empty, or its comparator does |
222 |
< |
* not tolerate <tt>null</tt> keys. |
219 |
> |
* with the elements currently in the set |
220 |
> |
* @throws NullPointerException if the specified element is null and |
221 |
> |
* this set uses natural ordering and is non-empty, or its |
222 |
> |
* comparator does not permit null elements |
223 |
|
*/ |
224 |
< |
public boolean add(E o) { |
225 |
< |
return m.put(o, PRESENT)==null; |
224 |
> |
public boolean add(E e) { |
225 |
> |
return m.put(e, PRESENT)==null; |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
|
* Removes the specified element from this set if it is present. |
230 |
|
* |
231 |
< |
* @param o object to be removed from this set, if present. |
232 |
< |
* @return <tt>true</tt> if the set contained the specified element. |
232 |
< |
* |
231 |
> |
* @param o object to be removed from this set, if present |
232 |
> |
* @return <tt>true</tt> if the set contained the specified element |
233 |
|
* @throws ClassCastException if the specified object cannot be compared |
234 |
< |
* with the elements currently in the set. |
235 |
< |
* @throws NullPointerException o is <tt>null</tt> and this map |
236 |
< |
* uses natural ordering and is non-empty, or its comparator does |
237 |
< |
* not tolerate <tt>null</tt> keys. |
234 |
> |
* with the elements currently in the set |
235 |
> |
* @throws NullPointerException if the specified element is null and |
236 |
> |
* this set uses natural ordering and is non-empty, or its |
237 |
> |
* comparator does not permit null elements |
238 |
|
*/ |
239 |
|
public boolean remove(Object o) { |
240 |
|
return m.remove(o)==PRESENT; |
242 |
|
|
243 |
|
/** |
244 |
|
* Removes all of the elements from this set. |
245 |
+ |
* The set will be empty after this call returns. |
246 |
|
*/ |
247 |
|
public void clear() { |
248 |
|
m.clear(); |
252 |
|
* Adds all of the elements in the specified collection to this set. |
253 |
|
* |
254 |
|
* @param c elements to be added |
255 |
< |
* @return <tt>true</tt> if this set changed as a result of the call. |
255 |
< |
* |
255 |
> |
* @return <tt>true</tt> if this set changed as a result of the call |
256 |
|
* @throws ClassCastException if the elements provided cannot be compared |
257 |
< |
* with the elements currently in the set. |
258 |
< |
* @throws NullPointerException of the specified collection is |
259 |
< |
* <tt>null</tt> or if any element is <tt>null</tt> and this map |
260 |
< |
* uses natural ordering, or its comparator does not tolerate |
261 |
< |
* <tt>null</tt> keys. |
257 |
> |
* with the elements currently in the set |
258 |
> |
* @throws NullPointerException if the specified collection is null or |
259 |
> |
* if any element is null and this set uses natural ordering, or |
260 |
> |
* its comparator does not permit null elements |
261 |
|
*/ |
262 |
|
public boolean addAll(Collection<? extends E> c) { |
263 |
|
// Use linear-time version if applicable |
264 |
|
if (m.size()==0 && c.size() > 0 && |
265 |
|
c instanceof SortedSet && |
266 |
|
m instanceof TreeMap) { |
267 |
< |
SortedSet<Map.Entry<E, Object>> set = (SortedSet<Map.Entry<E, Object>>) (SortedSet) c; |
267 |
> |
SortedSet<? extends E> set = (SortedSet<? extends E>) c; |
268 |
|
TreeMap<E,Object> map = (TreeMap<E, Object>) m; |
269 |
< |
Comparator<? super E> cc = (Comparator<E>) set.comparator(); |
269 |
> |
Comparator<? super E> cc = (Comparator<? super E>) set.comparator(); |
270 |
|
Comparator<? super E> mc = map.comparator(); |
271 |
|
if (cc==mc || (cc != null && cc.equals(mc))) { |
272 |
|
map.addAllForTreeSet(set, PRESENT); |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
< |
* Returns a view of the portion of this set whose elements range from |
282 |
< |
* <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>, exclusive. (If |
283 |
< |
* <tt>fromElement</tt> and <tt>toElement</tt> are equal, the returned |
284 |
< |
* sorted set is empty.) The returned sorted set is backed by this set, |
285 |
< |
* so changes in the returned sorted set are reflected in this set, and |
286 |
< |
* vice-versa. The returned sorted set supports all optional Set |
287 |
< |
* operations.<p> |
288 |
< |
* |
289 |
< |
* The sorted set returned by this method will throw an |
290 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert an |
291 |
< |
* element outside the specified range.<p> |
292 |
< |
* |
293 |
< |
* Note: this method always returns a <i>half-open range</i> (which |
294 |
< |
* includes its low endpoint but not its high endpoint). If you need a |
295 |
< |
* <i>closed range</i> (which includes both endpoints), and the element |
296 |
< |
* type allows for calculation of the successor of a specified value, |
297 |
< |
* merely request the subrange from <tt>lowEndpoint</tt> to |
298 |
< |
* <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt> |
299 |
< |
* is a sorted set of strings. The following idiom obtains a view |
300 |
< |
* containing all of the strings in <tt>s</tt> from <tt>low</tt> to |
301 |
< |
* <tt>high</tt>, inclusive: <pre> |
302 |
< |
* NavigableSet sub = s.subSet(low, high+"\0"); |
303 |
< |
* </pre> |
304 |
< |
* |
305 |
< |
* A similar technique can be used to generate an <i>open range</i> (which |
306 |
< |
* contains neither endpoint). The following idiom obtains a view |
307 |
< |
* containing all of the strings in <tt>s</tt> from <tt>low</tt> to |
308 |
< |
* <tt>high</tt>, exclusive: <pre> |
309 |
< |
* NavigableSet sub = s.subSet(low+"\0", high); |
310 |
< |
* </pre> |
311 |
< |
* |
312 |
< |
* @param fromElement low endpoint (inclusive) of the subSet. |
313 |
< |
* @param toElement high endpoint (exclusive) of the subSet. |
314 |
< |
* @return a view of the portion of this set whose elements range from |
315 |
< |
* <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>, |
316 |
< |
* exclusive. |
317 |
< |
* @throws ClassCastException if <tt>fromElement</tt> and |
318 |
< |
* <tt>toElement</tt> cannot be compared to one another using |
319 |
< |
* this set's comparator (or, if the set has no comparator, |
320 |
< |
* using natural ordering). |
321 |
< |
* @throws IllegalArgumentException if <tt>fromElement</tt> is greater than |
322 |
< |
* <tt>toElement</tt>. |
280 |
> |
* @throws ClassCastException {@inheritDoc} |
281 |
|
* @throws NullPointerException if <tt>fromElement</tt> or |
282 |
< |
* <tt>toElement</tt> is <tt>null</tt> and this set uses natural |
283 |
< |
* order, or its comparator does not tolerate <tt>null</tt> |
284 |
< |
* elements. |
285 |
< |
*/ |
286 |
< |
public NavigableSet<E> subSet(E fromElement, E toElement) { |
287 |
< |
return new TreeSet<E>(m.subMap(fromElement, toElement)); |
330 |
< |
} |
331 |
< |
|
332 |
< |
/** |
333 |
< |
* Returns a view of the portion of this set whose elements are strictly |
334 |
< |
* less than <tt>toElement</tt>. The returned sorted set is backed by |
335 |
< |
* this set, so changes in the returned sorted set are reflected in this |
336 |
< |
* set, and vice-versa. The returned sorted set supports all optional set |
337 |
< |
* operations.<p> |
338 |
< |
* |
339 |
< |
* The sorted set returned by this method will throw an |
340 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert an |
341 |
< |
* element greater than or equal to <tt>toElement</tt>.<p> |
342 |
< |
* |
343 |
< |
* Note: this method always returns a view that does not contain its |
344 |
< |
* (high) endpoint. If you need a view that does contain this endpoint, |
345 |
< |
* and the element type allows for calculation of the successor of a |
346 |
< |
* specified value, merely request a headSet bounded by |
347 |
< |
* <tt>successor(highEndpoint)</tt>. For example, suppose that <tt>s</tt> |
348 |
< |
* is a sorted set of strings. The following idiom obtains a view |
349 |
< |
* containing all of the strings in <tt>s</tt> that are less than or equal |
350 |
< |
* to <tt>high</tt>: <pre> NavigableSet head = s.headSet(high+"\0");</pre> |
351 |
< |
* |
352 |
< |
* @param toElement high endpoint (exclusive) of the headSet. |
353 |
< |
* @return a view of the portion of this set whose elements are strictly |
354 |
< |
* less than toElement. |
355 |
< |
* @throws ClassCastException if <tt>toElement</tt> is not compatible |
356 |
< |
* with this set's comparator (or, if the set has no comparator, |
357 |
< |
* if <tt>toElement</tt> does not implement <tt>Comparable</tt>). |
358 |
< |
* @throws IllegalArgumentException if this set is itself a subSet, |
359 |
< |
* headSet, or tailSet, and <tt>toElement</tt> is not within the |
360 |
< |
* specified range of the subSet, headSet, or tailSet. |
361 |
< |
* @throws NullPointerException if <tt>toElement</tt> is <tt>null</tt> and |
362 |
< |
* this set uses natural ordering, or its comparator does |
363 |
< |
* not tolerate <tt>null</tt> elements. |
364 |
< |
*/ |
365 |
< |
public NavigableSet<E> headSet(E toElement) { |
366 |
< |
return new TreeSet<E>(m.headMap(toElement)); |
367 |
< |
} |
368 |
< |
|
369 |
< |
/** |
370 |
< |
* Returns a view of the portion of this set whose elements are |
371 |
< |
* greater than or equal to <tt>fromElement</tt>. The returned sorted set |
372 |
< |
* is backed by this set, so changes in the returned sorted set are |
373 |
< |
* reflected in this set, and vice-versa. The returned sorted set |
374 |
< |
* supports all optional set operations.<p> |
375 |
< |
* |
376 |
< |
* The sorted set returned by this method will throw an |
377 |
< |
* <tt>IllegalArgumentException</tt> if the user attempts to insert an |
378 |
< |
* element less than <tt>fromElement</tt>. |
379 |
< |
* |
380 |
< |
* Note: this method always returns a view that contains its (low) |
381 |
< |
* endpoint. If you need a view that does not contain this endpoint, and |
382 |
< |
* the element type allows for calculation of the successor of a specified |
383 |
< |
* value, merely request a tailSet bounded by |
384 |
< |
* <tt>successor(lowEndpoint)</tt>. For example, suppose that <tt>s</tt> |
385 |
< |
* is a sorted set of strings. The following idiom obtains a view |
386 |
< |
* containing all of the strings in <tt>s</tt> that are strictly greater |
387 |
< |
* than <tt>low</tt>: <pre> |
388 |
< |
* NavigableSet tail = s.tailSet(low+"\0"); |
389 |
< |
* </pre> |
390 |
< |
* |
391 |
< |
* @param fromElement low endpoint (inclusive) of the tailSet. |
392 |
< |
* @return a view of the portion of this set whose elements are |
393 |
< |
* greater than or equal to <tt>fromElement</tt>. |
394 |
< |
* @throws ClassCastException if <tt>fromElement</tt> is not compatible |
395 |
< |
* with this set's comparator (or, if the set has no comparator, |
396 |
< |
* if <tt>fromElement</tt> does not implement <tt>Comparable</tt>). |
397 |
< |
* @throws IllegalArgumentException if this set is itself a subSet, |
398 |
< |
* headSet, or tailSet, and <tt>fromElement</tt> is not within the |
399 |
< |
* specified range of the subSet, headSet, or tailSet. |
400 |
< |
* @throws NullPointerException if <tt>fromElement</tt> is <tt>null</tt> |
401 |
< |
* and this set uses natural ordering, or its comparator does |
402 |
< |
* not tolerate <tt>null</tt> elements. |
403 |
< |
*/ |
404 |
< |
public NavigableSet<E> tailSet(E fromElement) { |
405 |
< |
return new TreeSet<E>(m.tailMap(fromElement)); |
282 |
> |
* <tt>toElement</tt> is null and this set uses natural ordering, |
283 |
> |
* or its comparator does not permit null elements |
284 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
285 |
> |
*/ |
286 |
> |
public NavigableSet<E> navigableSubSet(E fromElement, E toElement) { |
287 |
> |
return new TreeSet<E>(m.navigableSubMap(fromElement, toElement)); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
< |
* Returns the comparator used to order this sorted set, or <tt>null</tt> |
292 |
< |
* if this tree set uses its elements natural ordering. |
291 |
> |
* @throws ClassCastException {@inheritDoc} |
292 |
> |
* @throws NullPointerException if <tt>toElement</tt> is null and |
293 |
> |
* this set uses natural ordering, or its comparator does |
294 |
> |
* not permit null elements |
295 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
296 |
> |
*/ |
297 |
> |
public NavigableSet<E> navigableHeadSet(E toElement) { |
298 |
> |
return new TreeSet<E>(m.navigableHeadMap(toElement)); |
299 |
> |
} |
300 |
> |
|
301 |
> |
/** |
302 |
> |
* @throws ClassCastException {@inheritDoc} |
303 |
> |
* @throws NullPointerException if <tt>fromElement</tt> is null and |
304 |
> |
* this set uses natural ordering, or its comparator does |
305 |
> |
* not permit null elements |
306 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
307 |
> |
*/ |
308 |
> |
public NavigableSet<E> navigableTailSet(E fromElement) { |
309 |
> |
return new TreeSet<E>(m.navigableTailMap(fromElement)); |
310 |
> |
} |
311 |
> |
|
312 |
> |
/** |
313 |
> |
* Equivalent to {@link #navigableSubSet} but with a return type |
314 |
> |
* conforming to the <tt>SortedSet</tt> interface. |
315 |
> |
* |
316 |
> |
* <p>{@inheritDoc} |
317 |
|
* |
318 |
< |
* @return the comparator used to order this sorted set, or <tt>null</tt> |
319 |
< |
* if this tree set uses its elements natural ordering. |
318 |
> |
* @throws ClassCastException {@inheritDoc} |
319 |
> |
* @throws NullPointerException if <tt>fromElement</tt> or |
320 |
> |
* <tt>toElement</tt> is null and this set uses natural ordering, |
321 |
> |
* or its comparator does not permit null elements |
322 |
> |
* @throws IllegalArgumentException {@inheritDoc} |
323 |
|
*/ |
324 |
+ |
public SortedSet<E> subSet(E fromElement, E toElement) { |
325 |
+ |
return new TreeSet<E>(m.navigableSubMap(fromElement, toElement)); |
326 |
+ |
} |
327 |
+ |
|
328 |
+ |
/** |
329 |
+ |
* Equivalent to {@link #navigableHeadSet} but with a return type |
330 |
+ |
* conforming to the <tt>SortedSet</tt> interface. |
331 |
+ |
* |
332 |
+ |
* <p>{@inheritDoc} |
333 |
+ |
* |
334 |
+ |
* @throws ClassCastException {@inheritDoc} |
335 |
+ |
* @throws NullPointerException if <tt>toElement</tt> is null |
336 |
+ |
* and this set uses natural ordering, or its comparator does |
337 |
+ |
* not permit null elements |
338 |
+ |
* @throws IllegalArgumentException {@inheritDoc} |
339 |
+ |
*/ |
340 |
+ |
public SortedSet<E> headSet(E toElement) { |
341 |
+ |
return new TreeSet<E>(m.navigableHeadMap(toElement)); |
342 |
+ |
} |
343 |
+ |
|
344 |
+ |
/** |
345 |
+ |
* Equivalent to {@link #navigableTailSet} but with a return type |
346 |
+ |
* conforming to the <tt>SortedSet</tt> interface. |
347 |
+ |
* |
348 |
+ |
* <p>{@inheritDoc} |
349 |
+ |
* |
350 |
+ |
* @throws ClassCastException {@inheritDoc} |
351 |
+ |
* @throws NullPointerException if <tt>fromElement</tt> is null |
352 |
+ |
* and this set uses natural ordering, or its comparator does |
353 |
+ |
* not permit null elements |
354 |
+ |
* @throws IllegalArgumentException {@inheritDoc} |
355 |
+ |
*/ |
356 |
+ |
public SortedSet<E> tailSet(E fromElement) { |
357 |
+ |
return new TreeSet<E>(m.navigableTailMap(fromElement)); |
358 |
+ |
} |
359 |
+ |
|
360 |
|
public Comparator<? super E> comparator() { |
361 |
|
return m.comparator(); |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
< |
* Returns the first (lowest) element currently in this sorted set. |
421 |
< |
* |
422 |
< |
* @return the first (lowest) element currently in this sorted set. |
423 |
< |
* @throws NoSuchElementException sorted set is empty. |
365 |
> |
* @throws NoSuchElementException {@inheritDoc} |
366 |
|
*/ |
367 |
|
public E first() { |
368 |
|
return m.firstKey(); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* Returns the last (highest) element currently in this sorted set. |
431 |
< |
* |
432 |
< |
* @return the last (highest) element currently in this sorted set. |
433 |
< |
* @throws NoSuchElementException sorted set is empty. |
372 |
> |
* @throws NoSuchElementException {@inheritDoc} |
373 |
|
*/ |
374 |
|
public E last() { |
375 |
|
return m.lastKey(); |
377 |
|
|
378 |
|
// NavigableSet API methods |
379 |
|
|
380 |
+ |
/** |
381 |
+ |
* @throws ClassCastException {@inheritDoc} |
382 |
+ |
* @throws NullPointerException if the specified element is null and |
383 |
+ |
* this set uses natural ordering and is non-empty, |
384 |
+ |
* or its comparator does not permit null elements |
385 |
+ |
*/ |
386 |
+ |
public E lower(E e) { |
387 |
+ |
return m.lowerKey(e); |
388 |
+ |
} |
389 |
+ |
|
390 |
+ |
/** |
391 |
+ |
* @throws ClassCastException {@inheritDoc} |
392 |
+ |
* @throws NullPointerException if the specified element is null and |
393 |
+ |
* this set uses natural ordering and is non-empty, |
394 |
+ |
* or its comparator does not permit null elements |
395 |
+ |
*/ |
396 |
+ |
public E floor(E e) { |
397 |
+ |
return m.floorKey(e); |
398 |
+ |
} |
399 |
|
|
400 |
|
/** |
401 |
< |
* Returns an element greater than or equal to the given element, or |
402 |
< |
* <tt>null</tt> if there is no such element. |
403 |
< |
* |
404 |
< |
* @param o the value to match |
447 |
< |
* @return an element greater than or equal to given element, or |
448 |
< |
* <tt>null</tt> if there is no such element. |
449 |
< |
* @throws ClassCastException if o cannot be compared with the elements |
450 |
< |
* currently in the set. |
451 |
< |
* @throws NullPointerException o is <tt>null</tt> and this map |
452 |
< |
* uses natural ordering and is non-empty, or its comparator does |
453 |
< |
* not tolerate <tt>null</tt> keys. |
454 |
< |
*/ |
455 |
< |
public E ceiling(E o) { |
456 |
< |
return m.ceilingKey(o); |
457 |
< |
} |
458 |
< |
|
459 |
< |
/** |
460 |
< |
* Returns an element strictly less than the given element, or |
461 |
< |
* <tt>null</tt> if there is no such element. |
462 |
< |
* |
463 |
< |
* @param o the value to match |
464 |
< |
* @return the greatest element less than the given element, or |
465 |
< |
* <tt>null</tt> if there is no such element. |
466 |
< |
* @throws ClassCastException if o cannot be compared with the elements |
467 |
< |
* currently in the set. |
468 |
< |
* @throws NullPointerException o is <tt>null</tt> and this map |
469 |
< |
* uses natural ordering and is non-empty, or its comparator does |
470 |
< |
* not tolerate <tt>null</tt> keys. |
471 |
< |
*/ |
472 |
< |
public E lower(E o) { |
473 |
< |
return m.lowerKey(o); |
474 |
< |
} |
475 |
< |
|
476 |
< |
/** |
477 |
< |
* Returns an element less than or equal to the given element, or |
478 |
< |
* <tt>null</tt> if there is no such element. |
479 |
< |
* |
480 |
< |
* @param o the value to match |
481 |
< |
* @return the greatest element less than or equal to given |
482 |
< |
* element, or <tt>null</tt> if there is no such element. |
483 |
< |
* @throws ClassCastException if o cannot be compared with the elements |
484 |
< |
* currently in the set. |
485 |
< |
* @throws NullPointerException o is <tt>null</tt> and this map |
486 |
< |
* uses natural ordering and is non-empty, or its comparator does |
487 |
< |
* not tolerate <tt>null</tt> keys. |
488 |
< |
*/ |
489 |
< |
public E floor(E o) { |
490 |
< |
return m.floorKey(o); |
491 |
< |
} |
492 |
< |
|
493 |
< |
/** |
494 |
< |
* Returns an element strictly greater than the given element, or |
495 |
< |
* <tt>null</tt> if there is no such element. |
496 |
< |
* |
497 |
< |
* @param o the value to match |
498 |
< |
* @return the least element greater than the given element, or |
499 |
< |
* <tt>null</tt> if there is no such element. |
500 |
< |
* @throws ClassCastException if o cannot be compared with the elements |
501 |
< |
* currently in the set. |
502 |
< |
* @throws NullPointerException o is <tt>null</tt> and this map |
503 |
< |
* uses natural ordering and is non-empty, or its comparator does |
504 |
< |
* not tolerate <tt>null</tt> keys. |
401 |
> |
* @throws ClassCastException {@inheritDoc} |
402 |
> |
* @throws NullPointerException if the specified element is null and |
403 |
> |
* this set uses natural ordering and is non-empty, |
404 |
> |
* or its comparator does not permit null elements |
405 |
|
*/ |
406 |
< |
public E higher(E o) { |
407 |
< |
return m.higherKey(o); |
406 |
> |
public E ceiling(E e) { |
407 |
> |
return m.ceilingKey(e); |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* Retrieves and removes the first (lowest) element. |
412 |
< |
* |
413 |
< |
* @return the least element, or <tt>null</tt> if empty. |
411 |
> |
* @throws ClassCastException {@inheritDoc} |
412 |
> |
* @throws NullPointerException if the specified element is null and |
413 |
> |
* this set uses natural ordering and is non-empty, |
414 |
> |
* or its comparator does not permit null elements |
415 |
|
*/ |
416 |
+ |
public E higher(E e) { |
417 |
+ |
return m.higherKey(e); |
418 |
+ |
} |
419 |
+ |
|
420 |
|
public E pollFirst() { |
421 |
|
Map.Entry<E,?> e = m.pollFirstEntry(); |
422 |
|
return (e == null)? null : e.getKey(); |
423 |
|
} |
424 |
|
|
520 |
– |
/** |
521 |
– |
* Retrieves and removes the last (highest) element. |
522 |
– |
* |
523 |
– |
* @return the last element, or <tt>null</tt> if empty. |
524 |
– |
*/ |
425 |
|
public E pollLast() { |
426 |
|
Map.Entry<E,?> e = m.pollLastEntry(); |
427 |
|
return (e == null)? null : e.getKey(); |
431 |
|
* Returns a shallow copy of this <tt>TreeSet</tt> instance. (The elements |
432 |
|
* themselves are not cloned.) |
433 |
|
* |
434 |
< |
* @return a shallow copy of this set. |
434 |
> |
* @return a shallow copy of this set |
435 |
|
*/ |
436 |
|
public Object clone() { |
437 |
|
TreeSet<E> clone = null; |
451 |
|
* serialize it). |
452 |
|
* |
453 |
|
* @serialData Emits the comparator used to order this set, or |
454 |
< |
* <tt>null</tt> if it obeys its elements' natural ordering |
455 |
< |
* (Object), followed by the size of the set (the number of |
456 |
< |
* elements it contains) (int), followed by all of its |
457 |
< |
* elements (each an Object) in order (as determined by the |
458 |
< |
* set's Comparator, or by the elements' natural ordering if |
454 |
> |
* <tt>null</tt> if it obeys its elements' natural ordering |
455 |
> |
* (Object), followed by the size of the set (the number of |
456 |
> |
* elements it contains) (int), followed by all of its |
457 |
> |
* elements (each an Object) in order (as determined by the |
458 |
> |
* set's Comparator, or by the elements' natural ordering if |
459 |
|
* the set has no Comparator). |
460 |
|
*/ |
461 |
|
private void writeObject(java.io.ObjectOutputStream s) |
484 |
|
s.defaultReadObject(); |
485 |
|
|
486 |
|
// Read in Comparator |
487 |
< |
Comparator<E> c = (Comparator<E>) s.readObject(); |
487 |
> |
Comparator<? super E> c = (Comparator<? super E>) s.readObject(); |
488 |
|
|
489 |
|
// Create backing TreeMap |
490 |
|
TreeMap<E,Object> tm; |