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.10 by jsr166, Tue May 3 02:52:07 2005 UTC vs.
Revision 1.26 by jsr166, Sun May 20 07:54:01 2007 UTC

# Line 1 | Line 1
1   /*
2 < * %W% %E%
2 > * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
3 > * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5 < * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
6 < * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
5 > * This code is free software; you can redistribute it and/or modify it
6 > * under the terms of the GNU General Public License version 2 only, as
7 > * published by the Free Software Foundation.  Sun designates this
8 > * particular file as subject to the "Classpath" exception as provided
9 > * by Sun in the LICENSE file that accompanied this code.
10 > *
11 > * This code is distributed in the hope that it will be useful, but WITHOUT
12 > * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 > * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 > * version 2 for more details (a copy is included in the LICENSE file that
15 > * accompanied this code).
16 > *
17 > * You should have received a copy of the GNU General Public License version
18 > * 2 along with this work; if not, write to the Free Software Foundation,
19 > * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 > *
21 > * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 > * CA 95054 USA or visit www.sun.com if you need additional information or
23 > * have any questions.
24   */
25  
26   package java.util;
27  
28   /**
29 < * This class implements the <tt>Set</tt> interface, backed by a
30 < * <tt>TreeMap</tt> instance.  This class guarantees that the sorted set will
31 < * be in ascending element order, sorted according to the <i>natural order</i>
32 < * of the elements (see <tt>Comparable</tt>), or by the comparator provided at
15 < * set creation time, depending on which constructor is used.<p>
29 > * A {@link NavigableSet} implementation based on a {@link TreeMap}.
30 > * The elements are ordered using their {@linkplain Comparable natural
31 > * ordering}, or by a {@link Comparator} provided at set creation
32 > * time, depending on which constructor is used.
33   *
34 < * This implementation provides guaranteed log(n) time cost for the basic
35 < * operations (<tt>add</tt>, <tt>remove</tt> and <tt>contains</tt>).<p>
34 > * <p>This implementation provides guaranteed log(n) time cost for the basic
35 > * operations ({@code add}, {@code remove} and {@code contains}).
36   *
37 < * Note that the ordering maintained by a set (whether or not an explicit
37 > * <p>Note that the ordering maintained by a set (whether or not an explicit
38   * comparator is provided) must be <i>consistent with equals</i> if it is to
39 < * correctly implement the <tt>Set</tt> interface.  (See <tt>Comparable</tt>
40 < * or <tt>Comparator</tt> for a precise definition of <i>consistent with
41 < * equals</i>.)  This is so because the <tt>Set</tt> interface is defined in
42 < * terms of the <tt>equals</tt> operation, but a <tt>TreeSet</tt> instance
43 < * performs all key comparisons using its <tt>compareTo</tt> (or
44 < * <tt>compare</tt>) method, so two keys that are deemed equal by this method
39 > * correctly implement the {@code Set} interface.  (See {@code Comparable}
40 > * or {@code Comparator} for a precise definition of <i>consistent with
41 > * equals</i>.)  This is so because the {@code Set} interface is defined in
42 > * terms of the {@code equals} operation, but a {@code TreeSet} instance
43 > * performs all element comparisons using its {@code compareTo} (or
44 > * {@code compare}) method, so two elements that are deemed equal by this method
45   * are, from the standpoint of the set, equal.  The behavior of a set
46   * <i>is</i> well-defined even if its ordering is inconsistent with equals; it
47 < * just fails to obey the general contract of the <tt>Set</tt> interface.<p>
47 > * just fails to obey the general contract of the {@code Set} interface.
48   *
49 < * <b>Note that this implementation is not synchronized.</b> If multiple
50 < * threads access a set concurrently, and at least one of the threads modifies
51 < * the set, it <i>must</i> be synchronized externally.  This is typically
52 < * accomplished by synchronizing on some object that naturally encapsulates
53 < * the set.  If no such object exists, the set should be "wrapped" using the
54 < * <tt>Collections.synchronizedSet</tt> method.  This is best done at creation
55 < * time, to prevent accidental unsynchronized access to the set: <pre>
56 < *     SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
57 < * </pre><p>
49 > * <p><strong>Note that this implementation is not synchronized.</strong>
50 > * If multiple threads access a tree set concurrently, and at least one
51 > * of the threads modifies the set, it <i>must</i> be synchronized
52 > * externally.  This is typically accomplished by synchronizing on some
53 > * object that naturally encapsulates the set.
54 > * If no such object exists, the set should be "wrapped" using the
55 > * {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}
56 > * method.  This is best done at creation time, to prevent accidental
57 > * unsynchronized access to the set: <pre>
58 > *   SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));</pre>
59   *
60 < * The Iterators returned by this class's <tt>iterator</tt> method are
60 > * <p>The iterators returned by this class's {@code iterator} method are
61   * <i>fail-fast</i>: if the set is modified at any time after the iterator is
62 < * created, in any way except through the iterator's own <tt>remove</tt>
63 < * method, the iterator will throw a <tt>ConcurrentModificationException</tt>.
62 > * created, in any way except through the iterator's own {@code remove}
63 > * method, the iterator will throw a {@link ConcurrentModificationException}.
64   * Thus, in the face of concurrent modification, the iterator fails quickly
65   * and cleanly, rather than risking arbitrary, non-deterministic behavior at
66   * an undetermined time in the future.
# Line 50 | Line 68 | package java.util;
68   * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
69   * as it is, generally speaking, impossible to make any hard guarantees in the
70   * presence of unsynchronized concurrent modification.  Fail-fast iterators
71 < * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
71 > * throw {@code ConcurrentModificationException} on a best-effort basis.
72   * Therefore, it would be wrong to write a program that depended on this
73   * exception for its correctness:   <i>the fail-fast behavior of iterators
74   * should be used only to detect bugs.</i>
75   *
76   * <p>This class is a member of the
77 < * <a href="{@docRoot}/../guide/collections/index.html">
77 > * <a href="{@docRoot}/../technotes/guides/collections/index.html">
78   * Java Collections Framework</a>.
79   *
80 + * @param <E> the type of elements maintained by this set
81 + *
82   * @author  Josh Bloch
83   * @version %I%, %G%
84   * @see     Collection
# Line 66 | Line 86 | package java.util;
86   * @see     HashSet
87   * @see     Comparable
88   * @see     Comparator
69 * @see     Collections#synchronizedSortedSet(SortedSet)
89   * @see     TreeMap
90   * @since   1.2
91   */
92  
93 < public class TreeSet<E>
75 <    extends AbstractSet<E>
93 > public class TreeSet<E> extends AbstractSet<E>
94      implements NavigableSet<E>, Cloneable, java.io.Serializable
95   {
96 <    private transient NavigableMap<E,Object> m; // The backing Map
96 >    /**
97 >     * The backing map.
98 >     */
99 >    private transient NavigableMap<E,Object> m;
100  
101      // Dummy value to associate with an Object in the backing Map
102      private static final Object PRESENT = new Object();
103  
104      /**
105 <     * Constructs a set backed by the specified sorted map.
105 >     * Constructs a set backed by the specified navigable map.
106       */
107 <    private TreeSet(NavigableMap<E,Object> m) {
107 >    TreeSet(NavigableMap<E,Object> m) {
108          this.m = m;
109      }
110  
111      /**
112 <     * Constructs a new, empty set, sorted according to the elements' natural
113 <     * order.  All elements inserted into the set must implement the
114 <     * <tt>Comparable</tt> interface.  Furthermore, all such elements must be
115 <     * <i>mutually comparable</i>: <tt>e1.compareTo(e2)</tt> must not throw a
116 <     * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
117 <     * <tt>e2</tt> in the set.  If the user attempts to add an element to the
118 <     * set that violates this constraint (for example, the user attempts to
119 <     * add a string element to a set whose elements are integers), the
120 <     * <tt>add(Object)</tt> call will throw a <tt>ClassCastException</tt>.
121 <     *
122 <     * @see Comparable
112 >     * Constructs a new, empty tree set, sorted according to the
113 >     * natural ordering of its elements.  All elements inserted into
114 >     * the set must implement the {@link Comparable} interface.
115 >     * Furthermore, all such elements must be <i>mutually
116 >     * comparable</i>: {@code e1.compareTo(e2)} must not throw a
117 >     * {@code ClassCastException} for any elements {@code e1} and
118 >     * {@code e2} in the set.  If the user attempts to add an element
119 >     * to the set that violates this constraint (for example, the user
120 >     * attempts to add a string element to a set whose elements are
121 >     * integers), the {@code add} call will throw a
122 >     * {@code ClassCastException}.
123       */
124      public TreeSet() {
125          this(new TreeMap<E,Object>());
126      }
127  
128      /**
129 <     * Constructs a new, empty set, sorted according to the specified
129 >     * Constructs a new, empty tree set, sorted according to the specified
130       * comparator.  All elements inserted into the set must be <i>mutually
131 <     * comparable</i> by the specified comparator: <tt>comparator.compare(e1,
132 <     * e2)</tt> must not throw a <tt>ClassCastException</tt> for any elements
133 <     * <tt>e1</tt> and <tt>e2</tt> in the set.  If the user attempts to add
131 >     * comparable</i> by the specified comparator: {@code comparator.compare(e1,
132 >     * e2)} must not throw a {@code ClassCastException} for any elements
133 >     * {@code e1} and {@code e2} in the set.  If the user attempts to add
134       * an element to the set that violates this constraint, the
135 <     * <tt>add(Object)</tt> call will throw a <tt>ClassCastException</tt>.
135 >     * {@code add} call will throw a {@code ClassCastException}.
136       *
137 <     * @param c the comparator that will be used to sort this set.  A
138 <     *        <tt>null</tt> value indicates that the elements' <i>natural
139 <     *        ordering</i> should be used.
137 >     * @param comparator the comparator that will be used to order this set.
138 >     *        If {@code null}, the {@linkplain Comparable natural
139 >     *        ordering} of the elements will be used.
140       */
141 <    public TreeSet(Comparator<? super E> c) {
142 <        this(new TreeMap<E,Object>(c));
141 >    public TreeSet(Comparator<? super E> comparator) {
142 >        this(new TreeMap<E,Object>(comparator));
143      }
144  
145      /**
146 <     * Constructs a new set containing the elements in the specified
147 <     * collection, sorted according to the elements' <i>natural order</i>.
148 <     * All keys inserted into the set must implement the <tt>Comparable</tt>
149 <     * interface.  Furthermore, all such keys must be <i>mutually
150 <     * comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw a
151 <     * <tt>ClassCastException</tt> for any elements <tt>k1</tt> and
152 <     * <tt>k2</tt> in the set.
146 >     * Constructs a new tree set containing the elements in the specified
147 >     * collection, sorted according to the <i>natural ordering</i> of its
148 >     * elements.  All elements inserted into the set must implement the
149 >     * {@link Comparable} interface.  Furthermore, all such elements must be
150 >     * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
151 >     * {@code ClassCastException} for any elements {@code e1} and
152 >     * {@code e2} in the set.
153       *
154 <     * @param c The elements that will comprise the new set.
155 <     *
156 <     * @throws ClassCastException if the keys in the specified collection are
157 <     *         not comparable, or are not mutually comparable.
137 <     * @throws NullPointerException if the specified collection is null.
154 >     * @param c collection whose elements will comprise the new set
155 >     * @throws ClassCastException if the elements in {@code c} are
156 >     *         not {@link Comparable}, or are not mutually comparable
157 >     * @throws NullPointerException if the specified collection is null
158       */
159      public TreeSet(Collection<? extends E> c) {
160          this();
# Line 142 | Line 162 | public class TreeSet<E>
162      }
163  
164      /**
165 <     * Constructs a new set containing the same elements as the specified
166 <     * sorted set, sorted according to the same ordering.
165 >     * Constructs a new tree set containing the same elements and
166 >     * using the same ordering as the specified sorted set.
167       *
168 <     * @param s sorted set whose elements will comprise the new set.
169 <     * @throws NullPointerException if the specified sorted set is null.
168 >     * @param s sorted set whose elements will comprise the new set
169 >     * @throws NullPointerException if the specified sorted set is null
170       */
171      public TreeSet(SortedSet<E> s) {
172          this(s.comparator());
# Line 154 | Line 174 | public class TreeSet<E>
174      }
175  
176      /**
177 <     * Returns an iterator over the elements in this set.  The elements
158 <     * are returned in ascending order.
177 >     * Returns an iterator over the elements in this set in ascending order.
178       *
179 <     * @return an iterator over the elements in this set.
179 >     * @return an iterator over the elements in this set in ascending order
180       */
181      public Iterator<E> iterator() {
182 <        return m.keySet().iterator();
182 >        return m.navigableKeySet().iterator();
183      }
184  
185      /**
186 <     * Returns an iterator over the elements in this set.  The elements
168 <     * are returned in descending order.
186 >     * Returns an iterator over the elements in this set in descending order.
187       *
188 <     * @return an iterator over the elements in this set.
188 >     * @return an iterator over the elements in this set in descending order
189 >     * @since 1.6
190       */
191      public Iterator<E> descendingIterator() {
192          return m.descendingKeySet().iterator();
193      }
194  
195      /**
196 +     * @since 1.6
197 +     */
198 +    public NavigableSet<E> descendingSet() {
199 +        return new TreeSet(m.descendingMap());
200 +    }
201 +
202 +    /**
203       * Returns the number of elements in this set (its cardinality).
204       *
205 <     * @return the number of elements in this set (its cardinality).
205 >     * @return the number of elements in this set (its cardinality)
206       */
207      public int size() {
208          return m.size();
209      }
210  
211      /**
212 <     * Returns <tt>true</tt> if this set contains no elements.
212 >     * Returns {@code true} if this set contains no elements.
213       *
214 <     * @return <tt>true</tt> if this set contains no elements.
214 >     * @return {@code true} if this set contains no elements
215       */
216      public boolean isEmpty() {
217          return m.isEmpty();
218      }
219  
220      /**
221 <     * Returns <tt>true</tt> if this set contains the specified element.
222 <     *
223 <     * @param o the object to be checked for containment in this set.
224 <     * @return <tt>true</tt> if this set contains the specified element.
221 >     * Returns {@code true} if this set contains the specified element.
222 >     * More formally, returns {@code true} if and only if this set
223 >     * contains an element {@code e} such that
224 >     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
225       *
226 +     * @param o object to be checked for containment in this set
227 +     * @return {@code true} if this set contains the specified element
228       * @throws ClassCastException if the specified object cannot be compared
229 <     *            with the elements currently in the set.
230 <     * @throws NullPointerException if o is <tt>null</tt> and this map
231 <     * uses natural ordering and is non-empty, or its comparator does
232 <     * not tolerate <tt>null</tt> keys.
229 >     *         with the elements currently in the set
230 >     * @throws NullPointerException if the specified element is null
231 >     *         and this set uses natural ordering, or its comparator
232 >     *         does not permit null elements
233       */
234      public boolean contains(Object o) {
235          return m.containsKey(o);
# Line 209 | Line 237 | public class TreeSet<E>
237  
238      /**
239       * Adds the specified element to this set if it is not already present.
240 <     *
241 <     * @param e element to be added to this set.
242 <     * @return <tt>true</tt> if the set did not already contain the specified
243 <     *         element.
244 <     *
240 >     * More formally, adds the specified element {@code e} to this set if
241 >     * the set contains no element {@code e2} such that
242 >     * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
243 >     * If this set already contains the element, the call leaves the set
244 >     * unchanged and returns {@code false}.
245 >     *
246 >     * @param e element to be added to this set
247 >     * @return {@code true} if this set did not already contain the specified
248 >     *         element
249       * @throws ClassCastException if the specified object cannot be compared
250 <     *            with the elements currently in the set.
251 <     * @throws NullPointerException if e is <tt>null</tt> and this map
252 <     * uses natural ordering and is non-empty, or its comparator does
253 <     * not tolerate <tt>null</tt> keys.
250 >     *         with the elements currently in this set
251 >     * @throws NullPointerException if the specified element is null
252 >     *         and this set uses natural ordering, or its comparator
253 >     *         does not permit null elements
254       */
255      public boolean add(E e) {
256          return m.put(e, PRESENT)==null;
# Line 226 | Line 258 | public class TreeSet<E>
258  
259      /**
260       * Removes the specified element from this set if it is present.
261 +     * More formally, removes an element {@code e} such that
262 +     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>,
263 +     * if this set contains such an element.  Returns {@code true} if
264 +     * this set contained the element (or equivalently, if this set
265 +     * changed as a result of the call).  (This set will not contain the
266 +     * element once the call returns.)
267       *
268 <     * @param o object to be removed from this set, if present.
269 <     * @return <tt>true</tt> if the set contained the specified element.
232 <     *
268 >     * @param o object to be removed from this set, if present
269 >     * @return {@code true} if this set contained the specified element
270       * @throws ClassCastException if the specified object cannot be compared
271 <     *            with the elements currently in the set.
272 <     * @throws NullPointerException if o is <tt>null</tt> and this map
273 <     * uses natural ordering and is non-empty, or its comparator does
274 <     * not tolerate <tt>null</tt> keys.
271 >     *         with the elements currently in this set
272 >     * @throws NullPointerException if the specified element is null
273 >     *         and this set uses natural ordering, or its comparator
274 >     *         does not permit null elements
275       */
276      public boolean remove(Object o) {
277          return m.remove(o)==PRESENT;
# Line 242 | Line 279 | public class TreeSet<E>
279  
280      /**
281       * Removes all of the elements from this set.
282 +     * The set will be empty after this call returns.
283       */
284      public void clear() {
285          m.clear();
# Line 250 | Line 288 | public class TreeSet<E>
288      /**
289       * Adds all of the elements in the specified collection to this set.
290       *
291 <     * @param c elements to be added
292 <     * @return <tt>true</tt> if this set changed as a result of the call.
255 <     *
291 >     * @param c collection containing elements to be added to this set
292 >     * @return {@code true} if this set changed as a result of the call
293       * @throws ClassCastException if the elements provided cannot be compared
294 <     *            with the elements currently in the set.
295 <     * @throws NullPointerException if the specified collection is
296 <     * <tt>null</tt> or if any element is <tt>null</tt> and this map
297 <     * uses natural ordering, or its comparator does not tolerate
261 <     * <tt>null</tt> keys.
294 >     *         with the elements currently in the set
295 >     * @throws NullPointerException if the specified collection is null or
296 >     *         if any element is null and this set uses natural ordering, or
297 >     *         its comparator does not permit null elements
298       */
299      public  boolean addAll(Collection<? extends E> c) {
300          // Use linear-time version if applicable
# Line 278 | Line 314 | public class TreeSet<E>
314      }
315  
316      /**
317 <     * Returns a view of the portion of this set whose elements range
318 <     * from <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>,
319 <     * exclusive.  (If <tt>fromElement</tt> and <tt>toElement</tt> are
320 <     * equal, the returned navigable set is empty.)  The returned
321 <     * navigable set is backed by this set, so changes in the returned
322 <     * navigable set are reflected in this set, and vice-versa.  The
323 <     * returned navigable set supports all optional Set operations.<p>
324 <     *
325 <     * The navigable set returned by this method will throw an
326 <     * <tt>IllegalArgumentException</tt> if the user attempts to insert an
327 <     * element outside the specified range.<p>
328 <     *
329 <     * Note: this method always returns a <i>half-open range</i>
330 <     * (which includes its low endpoint but not its high endpoint).
331 <     * If you need a <i>closed range</i> (which includes both
332 <     * endpoints), and the element type allows for calculation of the
333 <     * successor of a specified value, merely request the subrange
334 <     * from <tt>lowEndpoint</tt> to <tt>successor(highEndpoint)</tt>.
335 <     * For example, suppose that <tt>s</tt> is a navigable set of
336 <     * strings.  The following idiom obtains a view containing all of
337 <     * the strings in <tt>s</tt> from <tt>low</tt> to <tt>high</tt>,
338 <     * inclusive:
339 <     * <pre> NavigableSet sub = s.navigableSubSet(low, high+"\0");
340 <     * </pre>
341 <     *
342 <     * A similar technique can be used to generate an <i>open range</i> (which
343 <     * contains neither endpoint).  The following idiom obtains a view
344 <     * containing all of the strings in <tt>s</tt> from <tt>low</tt> to
345 <     * <tt>high</tt>, exclusive: <pre>
346 <     *     NavigableSet sub = s.navigableSubSet(low+"\0", high);
347 <     * </pre>
348 <     *
349 <     * @param fromElement low endpoint (inclusive) of the range.
350 <     * @param toElement high endpoint (exclusive) of the range.
351 <     * @return a view of the portion of this set whose elements range from
352 <     *         <tt>fromElement</tt>, inclusive, to <tt>toElement</tt>,
353 <     *         exclusive.
354 <     * @throws ClassCastException if <tt>fromElement</tt> and
355 <     *         <tt>toElement</tt> cannot be compared to one another using
356 <     *         this set's comparator (or, if the set has no comparator,
357 <     *         using natural ordering).
358 <     * @throws IllegalArgumentException if <tt>fromElement</tt> is greater than
359 <     *         <tt>toElement</tt>.
324 <     * @throws NullPointerException if <tt>fromElement</tt> or
325 <     *         <tt>toElement</tt> is <tt>null</tt> and this set uses natural
326 <     *         order, or its comparator does not tolerate <tt>null</tt>
327 <     *         elements.
328 <     */
329 <    public NavigableSet<E> navigableSubSet(E fromElement, E toElement) {
330 <        return new TreeSet<E>(m.navigableSubMap(fromElement, toElement));
331 <    }
332 <
333 <    /**
334 <     * Returns a view of the portion of this set whose elements are
335 <     * strictly less than <tt>toElement</tt>.  The returned navigable
336 <     * set is backed by this set, so changes in the returned navigable
337 <     * set are reflected in this set, and vice-versa.  The returned
338 <     * 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.
367 <     */
368 <    public NavigableSet<E> navigableHeadSet(E toElement) {
369 <        return new TreeSet<E>(m.navigableHeadMap(toElement));
370 <    }
371 <
372 <    /**
373 <     * Returns a view of the portion of this set whose elements are
374 <     * greater than or equal to <tt>fromElement</tt>.  The returned
375 <     * navigable set is backed by this set, so changes in the returned
376 <     * navigable set are reflected in this set, and vice-versa.  The
377 <     * 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.
406 <     */
407 <    public NavigableSet<E> navigableTailSet(E fromElement) {
408 <        return new TreeSet<E>(m.navigableTailMap(fromElement));
409 <    }
410 <
411 <
412 <    /**
413 <     * Equivalent to <tt>navigableSubSet</tt> but with a return
414 <     * type conforming to the <tt>SortedSet</tt> interface.
415 <     * @param fromElement low endpoint (inclusive) of the range.
416 <     * @param toElement high endpoint (exclusive) of the range.
417 <     * @return a view of the portion of this set whose elements range from
418 <     *         <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>.
426 <     * @throws NullPointerException if <tt>fromElement</tt> or
427 <     *         <tt>toElement</tt> is <tt>null</tt> and this set uses natural
428 <     *         order, or its comparator does not tolerate <tt>null</tt>
429 <     *         elements.
317 >     * @throws ClassCastException {@inheritDoc}
318 >     * @throws NullPointerException if {@code fromElement} or {@code toElement}
319 >     *         is null and this set uses natural ordering, or its comparator
320 >     *         does not permit null elements
321 >     * @throws IllegalArgumentException {@inheritDoc}
322 >     * @since 1.6
323 >     */
324 >    public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
325 >                                  E toElement,   boolean toInclusive) {
326 >        return new TreeSet<E>(m.subMap(fromElement, fromInclusive,
327 >                                       toElement,   toInclusive));
328 >    }
329 >
330 >    /**
331 >     * @throws ClassCastException {@inheritDoc}
332 >     * @throws NullPointerException if {@code toElement} is null and
333 >     *         this set uses natural ordering, or its comparator does
334 >     *         not permit null elements
335 >     * @throws IllegalArgumentException {@inheritDoc}
336 >     * @since 1.6
337 >     */
338 >    public NavigableSet<E> headSet(E toElement, boolean inclusive) {
339 >        return new TreeSet<E>(m.headMap(toElement, inclusive));
340 >    }
341 >
342 >    /**
343 >     * @throws ClassCastException {@inheritDoc}
344 >     * @throws NullPointerException if {@code fromElement} is null and
345 >     *         this set uses natural ordering, or its comparator does
346 >     *         not permit null elements
347 >     * @throws IllegalArgumentException {@inheritDoc}
348 >     * @since 1.6
349 >     */
350 >    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
351 >        return new TreeSet<E>(m.tailMap(fromElement, inclusive));
352 >    }
353 >
354 >    /**
355 >     * @throws ClassCastException {@inheritDoc}
356 >     * @throws NullPointerException if {@code fromElement} or
357 >     *         {@code toElement} is null and this set uses natural ordering,
358 >     *         or its comparator does not permit null elements
359 >     * @throws IllegalArgumentException {@inheritDoc}
360       */
361      public SortedSet<E> subSet(E fromElement, E toElement) {
362 <        return new TreeSet<E>(m.navigableSubMap(fromElement, toElement));
362 >        return subSet(fromElement, true, toElement, false);
363      }
364  
365      /**
366 <     * Equivalent to <tt>navigableHeadSet</tt> but with a return
367 <     * type conforming to the <tt>SortedSet</tt> interface.
368 <     *
369 <     * @param toElement high endpoint (exclusive) of the headSet.
370 <     * @return a view of the portion of this set whose elements are strictly
441 <     *         less than <tt>toElement</tt>.
442 <     * @throws ClassCastException if <tt>toElement</tt> is not compatible
443 <     *         with this set's comparator (or, if the set has no comparator,
444 <     *         if <tt>toElement</tt> does not implement <tt>Comparable</tt>).
445 <     * @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.
366 >     * @throws ClassCastException {@inheritDoc}
367 >     * @throws NullPointerException if {@code toElement} is null
368 >     *         and this set uses natural ordering, or its comparator does
369 >     *         not permit null elements
370 >     * @throws IllegalArgumentException {@inheritDoc}
371       */
372      public SortedSet<E> headSet(E toElement) {
373 <        return new TreeSet<E>(m.navigableHeadMap(toElement));
373 >        return headSet(toElement, false);
374      }
375  
376      /**
377 <     * Equivalent to <tt>navigableTailSet</tt> but with a return
378 <     * type conforming to the <tt>SortedSet</tt> interface.
379 <     * @param fromElement low endpoint (inclusive) of the tailSet.
380 <     * @return a view of the portion of this set whose elements are
381 <     *         greater than or equal to <tt>fromElement</tt>.
462 <     * @throws ClassCastException if <tt>fromElement</tt> is not compatible
463 <     *         with this set's comparator (or, if the set has no comparator,
464 <     *         if <tt>fromElement</tt> does not implement <tt>Comparable</tt>).
465 <     * @throws IllegalArgumentException if this set is itself a subset,
466 <     *         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.
377 >     * @throws ClassCastException {@inheritDoc}
378 >     * @throws NullPointerException if {@code fromElement} is null
379 >     *         and this set uses natural ordering, or its comparator does
380 >     *         not permit null elements
381 >     * @throws IllegalArgumentException {@inheritDoc}
382       */
383      public SortedSet<E> tailSet(E fromElement) {
384 <        return new TreeSet<E>(m.navigableTailMap(fromElement));
384 >        return tailSet(fromElement, true);
385      }
386  
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     */
387      public Comparator<? super E> comparator() {
388          return m.comparator();
389      }
390  
391      /**
392 <     * 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.
392 >     * @throws NoSuchElementException {@inheritDoc}
393       */
394      public E first() {
395          return m.firstKey();
396      }
397  
398      /**
399 <     * 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.
399 >     * @throws NoSuchElementException {@inheritDoc}
400       */
401      public E last() {
402          return m.lastKey();
# Line 506 | Line 404 | public class TreeSet<E>
404  
405      // NavigableSet API methods
406  
509
407      /**
408 <     * Returns an element greater than or equal to the given element, or
409 <     * <tt>null</tt> if there is no such element.
410 <     *
411 <     * @param e the value to match
412 <     * @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 <
527 <    /**
528 <     * Returns an element strictly less than the given element, or
529 <     * <tt>null</tt> if there is no such element.
530 <     *
531 <     * @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.
408 >     * @throws ClassCastException {@inheritDoc}
409 >     * @throws NullPointerException if the specified element is null
410 >     *         and this set uses natural ordering, or its comparator
411 >     *         does not permit null elements
412 >     * @since 1.6
413       */
414      public E lower(E e) {
415          return m.lowerKey(e);
416      }
417  
418      /**
419 <     * Returns an element less than or equal to the given element, or
420 <     * <tt>null</tt> if there is no such element.
421 <     *
422 <     * @param e the value to match
423 <     * @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.
419 >     * @throws ClassCastException {@inheritDoc}
420 >     * @throws NullPointerException if the specified element is null
421 >     *         and this set uses natural ordering, or its comparator
422 >     *         does not permit null elements
423 >     * @since 1.6
424       */
425      public E floor(E e) {
426          return m.floorKey(e);
427      }
428  
429      /**
430 <     * Returns an element strictly greater than the given element, or
431 <     * <tt>null</tt> if there is no such element.
432 <     *
433 <     * @param e the value to match
434 <     * @return the least element greater than the given element, or
435 <     * <tt>null</tt> if there is no such element.
436 <     * @throws ClassCastException if e cannot be compared with the elements
437 <     *            currently in the set.
438 <     * @throws NullPointerException if e is <tt>null</tt> and this map
439 <     * uses natural ordering and is non-empty, or its comparator does
440 <     * not tolerate <tt>null</tt> keys.
430 >     * @throws ClassCastException {@inheritDoc}
431 >     * @throws NullPointerException if the specified element is null
432 >     *         and this set uses natural ordering, or its comparator
433 >     *         does not permit null elements
434 >     * @since 1.6
435 >     */
436 >    public E ceiling(E e) {
437 >        return m.ceilingKey(e);
438 >    }
439 >
440 >    /**
441 >     * @throws ClassCastException {@inheritDoc}
442 >     * @throws NullPointerException if the specified element is null
443 >     *         and this set uses natural ordering, or its comparator
444 >     *         does not permit null elements
445 >     * @since 1.6
446       */
447      public E higher(E e) {
448          return m.higherKey(e);
449      }
450  
451      /**
452 <     * Retrieves and removes the first (lowest) element.
580 <     *
581 <     * @return the least element, or <tt>null</tt> if empty.
452 >     * @since 1.6
453       */
454      public E pollFirst() {
455          Map.Entry<E,?> e = m.pollFirstEntry();
# Line 586 | Line 457 | public class TreeSet<E>
457      }
458  
459      /**
460 <     * Retrieves and removes the last (highest) element.
590 <     *
591 <     * @return the last element, or <tt>null</tt> if empty.
460 >     * @since 1.6
461       */
462      public E pollLast() {
463          Map.Entry<E,?> e = m.pollLastEntry();
# Line 596 | Line 465 | public class TreeSet<E>
465      }
466  
467      /**
468 <     * Returns a shallow copy of this <tt>TreeSet</tt> instance. (The elements
468 >     * Returns a shallow copy of this {@code TreeSet} instance. (The elements
469       * themselves are not cloned.)
470       *
471 <     * @return a shallow copy of this set.
471 >     * @return a shallow copy of this set
472       */
473      public Object clone() {
474          TreeSet<E> clone = null;
# Line 610 | Line 479 | public class TreeSet<E>
479          }
480  
481          clone.m = new TreeMap<E,Object>(m);
613
482          return clone;
483      }
484  
485      /**
486 <     * Save the state of the <tt>TreeSet</tt> instance to a stream (that is,
486 >     * Save the state of the {@code TreeSet} instance to a stream (that is,
487       * serialize it).
488       *
489       * @serialData Emits the comparator used to order this set, or
490 <     *             <tt>null</tt> if it obeys its elements' natural ordering
491 <     *             (Object), followed by the size of the set (the number of
492 <     *             elements it contains) (int), followed by all of its
493 <     *             elements (each an Object) in order (as determined by the
494 <     *             set's Comparator, or by the elements' natural ordering if
490 >     *             {@code null} if it obeys its elements' natural ordering
491 >     *             (Object), followed by the size of the set (the number of
492 >     *             elements it contains) (int), followed by all of its
493 >     *             elements (each an Object) in order (as determined by the
494 >     *             set's Comparator, or by the elements' natural ordering if
495       *             the set has no Comparator).
496       */
497      private void writeObject(java.io.ObjectOutputStream s)
# Line 643 | Line 511 | public class TreeSet<E>
511      }
512  
513      /**
514 <     * Reconstitute the <tt>TreeSet</tt> instance from a stream (that is,
514 >     * Reconstitute the {@code TreeSet} instance from a stream (that is,
515       * deserialize it).
516       */
517      private void readObject(java.io.ObjectInputStream s)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines