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

Comparing jsr166/src/main/java/util/TreeSet.java (file contents):
Revision 1.9 by jsr166, Mon May 2 21:44:01 2005 UTC vs.
Revision 1.16 by jsr166, Sun Jun 19 23:01:12 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines