NavigableSet
implementation based on a TreeMap
.
The elements are ordered using their natural
ordering, or by a Comparator
provided at set creation
time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the basic
operations (add
, remove
and contains
).
Note that the ordering maintained by a set (whether or not an explicit
comparator is provided) must be consistent with equals if it is to
correctly implement the Set
interface. (See Comparable
or Comparator
for a precise definition of consistent with
equals.) This is so because the Set
interface is defined in
terms of the equals
operation, but a TreeSet
instance
performs all element comparisons using its compareTo
(or
compare
) method, so two elements that are deemed equal by this method
are, from the standpoint of the set, equal. The behavior of a set
is well-defined even if its ordering is inconsistent with equals; it
just fails to obey the general contract of the Set
interface.
Note that this implementation is not synchronized.
If multiple threads access a tree set concurrently, and at least one
of the threads modifies the set, it must be synchronized
externally. This is typically accomplished by synchronizing on some
object that naturally encapsulates the set.
If no such object exists, the set should be "wrapped" using the
Collections.
method. This is best done at creation time, to prevent accidental
unsynchronized access to the set:
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
The iterators returned by this class's iterator
method are
fail-fast: if the set is modified at any time after the iterator is
created, in any way except through the iterator's own remove
method, the iterator will throw a ConcurrentModificationException
.
Thus, in the face of concurrent modification, the iterator fails quickly
and cleanly, rather than risking arbitrary, non-deterministic behavior at
an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed
as it is, generally speaking, impossible to make any hard guarantees in the
presence of unsynchronized concurrent modification. Fail-fast iterators
throw ConcurrentModificationException
on a best-effort basis.
Therefore, it would be wrong to write a program that depended on this
exception for its correctness: the fail-fast behavior of iterators
should be used only to detect bugs.
The addFirst
and addLast
methods of this class
throw UnsupportedOperationException
. The encounter order of elements is determined
by the comparison method; therefore, explicit positioning is not supported.
This class is a member of the Java Collections Framework.
Collection
, Set
, HashSet
, Comparable
, Comparator
, TreeMap
Modifier and Type | Field and Description |
---|---|
private transient NavigableMap | m
The backing map. |
private static final Object | |
private static final long |
Access | Constructor and Description |
---|---|
pack-priv | |
public | TreeSet()
Constructs a new, empty tree set, sorted according to the natural ordering of its elements. |
public | TreeSet(Comparator<? super E>
the comparator that will be used to order this set.
If comparator)null , the natural
ordering of the elements will be used.Constructs a new, empty tree set, sorted according to the specified comparator. |
public | TreeSet(Collection<? extends E>
collection whose elements will comprise the new set c)Constructs a new tree set containing the elements in the specified collection, sorted according to the natural ordering of its elements. |
public |
Modifier and Type | Method and Description |
---|---|
public boolean | Returns: true if this set did not already contain the specified
elementelement to be added to this set e)Overrides java. Implements java. Adds the specified element to this set if it is not already present. |
public boolean | Returns: true if this set changed as a result of the callcollection containing elements to be added to this set c)Overrides java. Implements java. Adds all of the elements in the specified collection to this set. |
public void | addFirst(E
the element to be added e)Overrides default java. Throws |
public void | addLast(E
the element to be added. e)Overrides default java. Throws |
public E | ceiling(E
the value to match e)Implements java. Returns the least element in this set greater than or equal to
the given element, or |
public void | clear()
Overrides java. Implements java. Removes all of the elements from this set. |
public Object | Returns: a shallow copy of this setOverrides java. Returns a shallow copy of this |
public Comparator | comparator()
Implements java. Returns the comparator used to order the elements in this set,
or |
public boolean | Returns: true if this set contains the specified elementobject to be checked for containment in this set o)Overrides java. Implements java. Returns |
public Iterator | Returns: an iterator over the elements in this set in descending orderImplements java. Returns an iterator over the elements in this set in descending order. |
public NavigableSet | descendingSet()
Implements java. Returns a reverse order view of the elements contained in this set. |
public E | first()
Implements java. Returns the first (lowest) element currently in this set. |
public E | floor(E
the value to match e)Implements java. Returns the greatest element in this set less than or equal to
the given element, or |
public NavigableSet | headSet(E
high endpoint of the returned set toElement, boolean true if the high endpoint
is to be included in the returned viewImplements java. Returns a view of the portion of this set whose elements are less than
(or equal to, if |
public SortedSet | headSet(E
high endpoint (exclusive) of the returned set toElement)Implements java. Returns a view of the portion of this set whose elements are
strictly less than |
public E | higher(E
the value to match e)Implements java. Returns the least element in this set strictly greater than the
given element, or |
public boolean | Returns: true if this set contains no elementsOverrides java. Implements java. Returns |
public Iterator | Returns: an iterator over the elements in this set in ascending orderImplements abstract java. Implements java. Returns an iterator over the elements in this set in ascending order. |
public E | last()
Implements java. Returns the last (highest) element currently in this set. |
public E | lower(E
the value to match e)Implements java. Returns the greatest element in this set strictly less than the
given element, or |
public E | pollFirst()
Implements java. Retrieves and removes the first (lowest) element,
or returns |
public E | pollLast()
Implements java. Retrieves and removes the last (highest) element,
or returns |
private void | readObject(ObjectInputStream s)
Reconstitute the |
public boolean | Returns: true if this set contained the specified elementobject to be removed from this set, if present o)Overrides java. Implements java. Removes the specified element from this set if it is present. |
public int | Returns: the number of elements in this set (its cardinality)Implements abstract java. Implements java. Returns the number of elements in this set (its cardinality). |
public Spliterator | Returns: aSpliterator over the elements in this setOverrides default java. Creates a late-binding
and fail-fast |
public NavigableSet | subSet(E
low endpoint of the returned set fromElement, boolean true if the low endpoint
is to be included in the returned viewhigh endpoint of the returned set toElement, boolean true if the high endpoint
is to be included in the returned viewImplements java. Returns a view of the portion of this set whose elements range from
|
public SortedSet | subSet(E
low endpoint (inclusive) of the returned set fromElement, E high endpoint (exclusive) of the returned set toElement)Implements java. Returns a view of the portion of this set whose elements range
from |
public NavigableSet | tailSet(E
low endpoint of the returned set fromElement, boolean true if the low endpoint
is to be included in the returned viewImplements java. Returns a view of the portion of this set whose elements are greater
than (or equal to, if |
public SortedSet | tailSet(E
low endpoint (inclusive) of the returned set fromElement)Implements java. Returns a view of the portion of this set whose elements are
greater than or equal to |
private void | writeObject(ObjectOutputStream s)
Save the state of the |
m | back to summary |
---|---|
private transient NavigableMap<E, Object> m The backing map. |
PRESENT | back to summary |
---|---|
private static final Object PRESENT |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID
|
TreeSet | back to summary |
---|---|
pack-priv TreeSet(NavigableMap<E, Object> m) Constructs a set backed by the specified navigable map. |
TreeSet | back to summary |
---|---|
public TreeSet() Constructs a new, empty tree set, sorted according to the
natural ordering of its elements. All elements inserted into
the set must implement the |
TreeSet | back to summary |
---|---|
public TreeSet(Comparator<? super E> comparator) Constructs a new, empty tree set, sorted according to the specified
comparator. All elements inserted into the set must be mutually
comparable by the specified comparator:
|
TreeSet | back to summary |
---|---|
public TreeSet(Collection<? extends E> c) Constructs a new tree set containing the elements in the specified
collection, sorted according to the natural ordering of its
elements. All elements inserted into the set must implement the
|
TreeSet | back to summary |
---|---|
public TreeSet(SortedSet<E> s) Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.
|
add | back to summary |
---|---|
public boolean add(E e) Overrides java. Implements java. Adds the specified element to this set if it is not already present.
More formally, adds the specified element
|
addAll | back to summary |
---|---|
public boolean addAll(Collection<? extends E> c) Overrides java. Implements java. Adds all of the elements in the specified collection to this set.
|
addFirst | back to summary |
---|---|
public void addFirst(E e) Overrides default java. Throws
|
addLast | back to summary |
---|---|
public void addLast(E e) Overrides default java. Throws
|
ceiling | back to summary |
---|---|
public E ceiling(E e) Implements java. Doc from java. Returns the least element in this set greater than or equal to
the given element, or
|
clear | back to summary |
---|---|
public void clear() Overrides java. Implements java. Removes all of the elements from this set. The set will be empty after this call returns. |
clone | back to summary |
---|---|
public Object clone() Overrides java. Returns a shallow copy of this
|
comparator | back to summary |
---|---|
public Comparator Implements java. Doc from java. Returns the comparator used to order the elements in this set,
or
|
contains | back to summary |
---|---|
public boolean contains(Object o) Overrides java. Implements java. Returns
|
descendingIterator | back to summary |
---|---|
public Iterator Implements java. Returns an iterator over the elements in this set in descending order.
|
descendingSet | back to summary |
---|---|
public NavigableSet Implements java. Doc from java. Returns a reverse order view of the elements contained in this set.
The descending set is backed by this set, so changes to the set are
reflected in the descending set, and vice-versa. If either set is
modified while an iteration over either set is in progress (except
through the iterator's own The returned set has an ordering equivalent to
|
first | back to summary |
---|---|
public E first() Implements java. Doc from java. Returns the first (lowest) element currently in this set.
|
floor | back to summary |
---|---|
public E floor(E e) Implements java. Doc from java. Returns the greatest element in this set less than or equal to
the given element, or
|
headSet | back to summary |
---|---|
public NavigableSet Implements java. Doc from java. Returns a view of the portion of this set whose elements are less than
(or equal to, if The returned set will throw an
|
headSet | back to summary |
---|---|
public SortedSet Implements java. Doc from java. Returns a view of the portion of this set whose elements are
strictly less than The returned set will throw an Equivalent to
|
higher | back to summary |
---|---|
public E higher(E e) Implements java. Doc from java. Returns the least element in this set strictly greater than the
given element, or
|
isEmpty | back to summary |
---|---|
public boolean isEmpty() Overrides java. Implements java. Returns
|
iterator | back to summary |
---|---|
public Iterator Implements abstract java. Implements java. Returns an iterator over the elements in this set in ascending order.
|
last | back to summary |
---|---|
public E last() Implements java. Doc from java. Returns the last (highest) element currently in this set.
|
lower | back to summary |
---|---|
public E lower(E e) Implements java. Doc from java. Returns the greatest element in this set strictly less than the
given element, or
|
pollFirst | back to summary |
---|---|
public E pollFirst() Implements java. Doc from java. Retrieves and removes the first (lowest) element,
or returns
|
pollLast | back to summary |
---|---|
public E pollLast() Implements java. Doc from java. Retrieves and removes the last (highest) element,
or returns
|
readObject | back to summary |
---|---|
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException Reconstitute the
|
remove | back to summary |
---|---|
public boolean remove(Object o) Overrides java. Implements java. Removes the specified element from this set if it is present.
More formally, removes an element
|
size | back to summary |
---|---|
public int size() Implements abstract java. Implements java. Returns the number of elements in this set (its cardinality).
|
spliterator | back to summary |
---|---|
public Spliterator Overrides default java. Creates a late-binding
and fail-fast The The spliterator's comparator (see
|
subSet | back to summary |
---|---|
public NavigableSet Implements java. Doc from java. Returns a view of the portion of this set whose elements range from
The returned set will throw an
|
subSet | back to summary |
---|---|
public SortedSet Implements java. Doc from java. Returns a view of the portion of this set whose elements range
from The returned set will throw an Equivalent to
|
tailSet | back to summary |
---|---|
public NavigableSet Implements java. Doc from java. Returns a view of the portion of this set whose elements are greater
than (or equal to, if The returned set will throw an
|
tailSet | back to summary |
---|---|
public SortedSet Implements java. Doc from java. Returns a view of the portion of this set whose elements are
greater than or equal to The returned set will throw an Equivalent to
|
writeObject | back to summary |
---|---|
private void writeObject(ObjectOutputStream s) throws IOException Save the state of the
|