1 *java.util.TreeSet* *TreeSet* ANavigableSetimplementation based on aTreeMap.
3 public class TreeSet<E>
4 extends |java.util.AbstractSet|
5 implements |java.util.NavigableSet|
9 |java.util.TreeSet_Description|
10 |java.util.TreeSet_Fields|
11 |java.util.TreeSet_Constructors|
12 |java.util.TreeSet_Methods|
14 ================================================================================
16 *java.util.TreeSet_Constructors*
17 |java.util.TreeSet()|Constructs a new, empty tree set, sorted according to the
18 |java.util.TreeSet(Collection<?extendsE>)|Constructs a new tree set containing
19 |java.util.TreeSet(Comparator<?superE>)|Constructs a new, empty tree set, sorte
20 |java.util.TreeSet(SortedSet<E>)|Constructs a new tree set containing the same
22 *java.util.TreeSet_Methods*
23 |java.util.TreeSet.add(E)|Adds the specified element to this set if it is not a
24 |java.util.TreeSet.addAll(Collection<?extendsE>)|Adds all of the elements in th
25 |java.util.TreeSet.ceiling(E)|
26 |java.util.TreeSet.clear()|Removes all of the elements from this set.
27 |java.util.TreeSet.clone()|Returns a shallow copy of thisTreeSetinstance.
28 |java.util.TreeSet.comparator()|
29 |java.util.TreeSet.contains(Object)|Returnstrueif this set contains the specifi
30 |java.util.TreeSet.descendingIterator()|Returns an iterator over the elements i
31 |java.util.TreeSet.descendingSet()|
32 |java.util.TreeSet.first()|
33 |java.util.TreeSet.floor(E)|
34 |java.util.TreeSet.headSet(E)|
35 |java.util.TreeSet.headSet(E,boolean)|
36 |java.util.TreeSet.higher(E)|
37 |java.util.TreeSet.isEmpty()|Returnstrueif this set contains no elements.
38 |java.util.TreeSet.iterator()|Returns an iterator over the elements in this set
39 |java.util.TreeSet.last()|
40 |java.util.TreeSet.lower(E)|
41 |java.util.TreeSet.pollFirst()|
42 |java.util.TreeSet.pollLast()|
43 |java.util.TreeSet.remove(Object)|Removes the specified element from this set i
44 |java.util.TreeSet.size()|Returns the number of elements in this set (its cardi
45 |java.util.TreeSet.subSet(E,boolean,E,boolean)|
46 |java.util.TreeSet.subSet(E,E)|
47 |java.util.TreeSet.tailSet(E)|
48 |java.util.TreeSet.tailSet(E,boolean)|
50 *java.util.TreeSet_Description*
52 A (|java.util.NavigableSet|) implementation based on a (|java.util.TreeMap|) .
53 The elements are ordered using their natural ordering(|java.lang.Comparable|) ,
54 or by a (|java.util.Comparator|) provided at set creation time, depending on
55 which constructor is used.
57 This implementation provides guaranteed log(n) time cost for the basic
58 operations (add,removeandcontains).
60 Note that the ordering maintained by a set (whether or not an explicit
61 comparator is provided) must be consistent with equals if it is to correctly
62 implement theSetinterface. (SeeComparableorComparatorfor a precise definition
63 of consistent with equals.) This is so because theSetinterface is defined in
64 terms of theequalsoperation, but aTreeSetinstance performs all element
65 comparisons using itscompareTo(orcompare) method, so two elements that are
66 deemed equal by this method are, from the standpoint of the set, equal. The
67 behavior of a set is well-defined even if its ordering is inconsistent with
68 equals; it just fails to obey the general contract of theSetinterface.
70 Note that this implementation is not synchronized. If multiple threads access a
71 tree set concurrently, and at least one of the threads modifies the set, it
72 must be synchronized externally. This is typically accomplished by
73 synchronizing on some object that naturally encapsulates the set. If no such
74 object exists, the set should be "wrapped" using the
75 Collections.synchronizedSortedSet(|java.util.Collections|) method. This is best
76 done at creation time, to prevent accidental unsynchronized access to the set:
78 SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
80 The iterators returned by this class'siteratormethod are fail-fast: if the set
81 is modified at any time after the iterator is created, in any way except
82 through the iterator's ownremovemethod, the iterator will throw a
83 (|java.util.ConcurrentModificationException|) . Thus, in the face of concurrent
84 modification, the iterator fails quickly and cleanly, rather than risking
85 arbitrary, non-deterministic behavior at an undetermined time in the future.
87 Note that the fail-fast behavior of an iterator cannot be guaranteed as it is,
88 generally speaking, impossible to make any hard guarantees in the presence of
89 unsynchronized concurrent modification. Fail-fast iterators
90 throwConcurrentModificationExceptionon a best-effort basis. Therefore, it would
91 be wrong to write a program that depended on this exception for its
92 correctness: the fail-fast behavior of iterators should be used only to detect
95 This class is a member of the <a
96 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
100 *java.util.TreeSet()*
104 Constructs a new, empty tree set, sorted according to the natural ordering of
105 its elements. All elements inserted into the set must implement the
106 (|java.lang.Comparable|) interface. Furthermore, all such elements must be
107 mutually comparable:e1.compareTo(e2)must not throw aClassCastExceptionfor any
108 elementse1ande2in the set. If the user attempts to add an element to the set
109 that violates this constraint (for example, the user attempts to add a string
110 element to a set whose elements are integers), theaddcall will throw
114 *java.util.TreeSet(Collection<?extendsE>)*
116 public TreeSet(java.util.Collection<? extends E> c)
118 Constructs a new tree set containing the elements in the specified collection,
119 sorted according to the natural ordering of its elements. All elements inserted
120 into the set must implement the (|java.lang.Comparable|) interface.
121 Furthermore, all such elements must be mutually comparable:e1.compareTo(e2)must
122 not throw aClassCastExceptionfor any elementse1ande2in the set.
124 c - collection whose elements will comprise the new set
126 *java.util.TreeSet(Comparator<?superE>)*
128 public TreeSet(java.util.Comparator<? super E> comparator)
130 Constructs a new, empty tree set, sorted according to the specified comparator.
131 All elements inserted into the set must be mutually comparable by the specified
132 comparator:comparator.compare(e1, e2)must not throw aClassCastExceptionfor any
133 elementse1ande2in the set. If the user attempts to add an element to the set
134 that violates this constraint, theaddcall will throw aClassCastException.
136 comparator - the comparator that will be used to order this set. If {@code null}, the
137 {@linkplain Comparable natural ordering} of the elements will be used.
139 *java.util.TreeSet(SortedSet<E>)*
141 public TreeSet(java.util.SortedSet<E> s)
143 Constructs a new tree set containing the same elements and using the same
144 ordering as the specified sorted set.
146 s - sorted set whose elements will comprise the new set
148 *java.util.TreeSet.add(E)*
150 public boolean add(E e)
152 Adds the specified element to this set if it is not already present. More
153 formally, adds the specified elementeto this set if the set contains no
154 elemente2such that (e==null?e2==null:e.equals(e2)). If this set already
155 contains the element, the call leaves the set unchanged and returnsfalse.
158 e - element to be added to this set
160 Returns: {@code true} if this set did not already contain the specified element
162 *java.util.TreeSet.addAll(Collection<?extendsE>)*
164 public boolean addAll(java.util.Collection<? extends E> c)
166 Adds all of the elements in the specified collection to this set.
169 c - collection containing elements to be added to this set
171 Returns: {@code true} if this set changed as a result of the call
173 *java.util.TreeSet.ceiling(E)*
175 public |E| ceiling(E e)
181 *java.util.TreeSet.clear()*
185 Removes all of the elements from this set. The set will be empty after this
190 *java.util.TreeSet.clone()*
192 public |java.lang.Object| clone()
194 Returns a shallow copy of thisTreeSetinstance. (The elements themselves are not
199 Returns: a shallow copy of this set
201 *java.util.TreeSet.comparator()*
203 public |java.util.Comparator|<? super E> comparator()
209 *java.util.TreeSet.contains(Object)*
211 public boolean contains(java.lang.Object o)
213 Returnstrueif this set contains the specified element. More formally,
214 returnstrueif and only if this set contains an elementesuch that
215 (o==null?e==null:o.equals(e)).
218 o - object to be checked for containment in this set
220 Returns: {@code true} if this set contains the specified element
222 *java.util.TreeSet.descendingIterator()*
224 public |java.util.Iterator|<E> descendingIterator()
226 Returns an iterator over the elements in this set in descending order.
230 Returns: an iterator over the elements in this set in descending order
232 *java.util.TreeSet.descendingSet()*
234 public |java.util.NavigableSet|<E> descendingSet()
240 *java.util.TreeSet.first()*
248 *java.util.TreeSet.floor(E)*
250 public |E| floor(E e)
256 *java.util.TreeSet.headSet(E)*
258 public |java.util.SortedSet|<E> headSet(E toElement)
264 *java.util.TreeSet.headSet(E,boolean)*
266 public |java.util.NavigableSet|<E> headSet(
274 *java.util.TreeSet.higher(E)*
276 public |E| higher(E e)
282 *java.util.TreeSet.isEmpty()*
284 public boolean isEmpty()
286 Returnstrueif this set contains no elements.
290 Returns: {@code true} if this set contains no elements
292 *java.util.TreeSet.iterator()*
294 public |java.util.Iterator|<E> iterator()
296 Returns an iterator over the elements in this set in ascending order.
300 Returns: an iterator over the elements in this set in ascending order
302 *java.util.TreeSet.last()*
310 *java.util.TreeSet.lower(E)*
312 public |E| lower(E e)
318 *java.util.TreeSet.pollFirst()*
320 public |E| pollFirst()
326 *java.util.TreeSet.pollLast()*
328 public |E| pollLast()
334 *java.util.TreeSet.remove(Object)*
336 public boolean remove(java.lang.Object o)
338 Removes the specified element from this set if it is present. More formally,
339 removes an elementesuch that (o==null?e==null:o.equals(e)), if this set
340 contains such an element. Returnstrueif this set contained the element (or
341 equivalently, if this set changed as a result of the call). (This set will not
342 contain the element once the call returns.)
345 o - object to be removed from this set, if present
347 Returns: {@code true} if this set contained the specified element
349 *java.util.TreeSet.size()*
353 Returns the number of elements in this set (its cardinality).
357 Returns: the number of elements in this set (its cardinality)
359 *java.util.TreeSet.subSet(E,boolean,E,boolean)*
361 public |java.util.NavigableSet|<E> subSet(
363 boolean fromInclusive,
371 *java.util.TreeSet.subSet(E,E)*
373 public |java.util.SortedSet|<E> subSet(
381 *java.util.TreeSet.tailSet(E)*
383 public |java.util.SortedSet|<E> tailSet(E fromElement)
389 *java.util.TreeSet.tailSet(E,boolean)*
391 public |java.util.NavigableSet|<E> tailSet(