public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable
| Constructor and Description |
|---|
TreeSet()
Constructs a new empty instance of
TreeSet which uses natural
ordering. |
TreeSet(Collection<? extends E> collection)
Constructs a new instance of
TreeSet which uses natural ordering
and containing the unique elements in the specified collection. |
TreeSet(Comparator<? super E> comparator)
Constructs a new empty instance of
TreeSet which uses the
specified comparator. |
TreeSet(SortedSet<E> set)
Constructs a new instance of
TreeSet containing the elements of
the specified SortedSet and using the same Comparator. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E object)
Adds the specified object to this
TreeSet. |
boolean |
addAll(Collection<? extends E> collection)
Adds the objects in the specified collection to this
TreeSet. |
E |
ceiling(E e)
Returns the least element in this set greater than or equal to
the given element, or
null if there is no such element. |
void |
clear()
Removes all elements from this
TreeSet, leaving it empty. |
Object |
clone()
Returns a new
TreeSet with the same elements, size and comparator
as this TreeSet. |
Comparator<? super E> |
comparator()
Returns the comparator used to compare elements in this
TreeSet. |
boolean |
contains(Object object)
Searches this
TreeSet for the specified object. |
Iterator<E> |
descendingIterator()
Returns an iterator over the elements in this set, in descending order.
|
NavigableSet<E> |
descendingSet()
Returns a reverse order view of the elements contained in this set.
|
E |
first()
Returns the first element in this set.
|
E |
floor(E e)
Returns the greatest element in this set less than or equal to
the given element, or
null if there is no such element. |
SortedSet<E> |
headSet(E end)
Returns a
SortedSet of the specified portion of this TreeSet which
contains elements less than the end element. |
NavigableSet<E> |
headSet(E end,
boolean endInclusive)
Returns a view of the portion of this set whose elements are less than
(or equal to, if
inclusive is true) toElement. |
E |
higher(E e)
Returns the least element in this set strictly greater than the
given element, or
null if there is no such element. |
boolean |
isEmpty()
Returns true if this
TreeSet has no element, otherwise false. |
Iterator<E> |
iterator()
Returns an Iterator on the elements of this
TreeSet. |
E |
last()
Returns the last element in this set.
|
E |
lower(E e)
Returns the greatest element in this set strictly less than the
given element, or
null if there is no such element. |
E |
pollFirst()
Retrieves and removes the first (lowest) element,
or returns
null if this set is empty. |
E |
pollLast()
Retrieves and removes the last (highest) element,
or returns
null if this set is empty. |
boolean |
remove(Object object)
Removes an occurrence of the specified object from this
TreeSet. |
int |
size()
Returns the number of elements in this
TreeSet. |
NavigableSet<E> |
subSet(E start,
boolean startInclusive,
E end,
boolean endInclusive)
Returns a view of the portion of this set whose elements range from
fromElement to toElement. |
SortedSet<E> |
subSet(E start,
E end)
Returns a
SortedSet of the specified portion of this TreeSet which
contains elements greater or equal to the start element but less than the
end element. |
SortedSet<E> |
tailSet(E start)
Returns a
SortedSet of the specified portion of this TreeSet which
contains elements greater or equal to the start element. |
NavigableSet<E> |
tailSet(E start,
boolean startInclusive)
Returns a view of the portion of this set whose elements are greater
than (or equal to, if
inclusive is true) fromElement. |
equals, hashCode, removeAllcontainsAll, retainAll, toArray, toArray, toStringfinalize, getClass, notify, notifyAll, wait, wait, waitspliteratorcontainsAll, copyOf, equals, hashCode, of, of, of, of, of, of, of, of, of, of, of, of, removeAll, retainAll, toArray, toArrayforEach, parallelStream, removeIf, stream, toArraypublic TreeSet()
TreeSet which uses natural
ordering.public TreeSet(Collection<? extends E> collection)
TreeSet which uses natural ordering
and containing the unique elements in the specified collection.collection - the collection of elements to add.ClassCastException - when an element in the collection does not implement the
Comparable interface, or the elements in the collection
cannot be compared.public TreeSet(Comparator<? super E> comparator)
TreeSet which uses the
specified comparator.comparator - the comparator to use.public boolean add(E object)
TreeSet.add in interface Collection<E>add in interface Set<E>add in class AbstractCollection<E>object - the object to add.true when this TreeSet did not already contain
the object, false otherwise.ClassCastException - when the object cannot be compared with the elements in this
TreeSet.NullPointerException - when the object is null and the comparator cannot handle
null.public boolean addAll(Collection<? extends E> collection)
TreeSet.addAll in interface Collection<E>addAll in interface Set<E>addAll in class AbstractCollection<E>collection - the collection of objects to add.true if this TreeSet was modified, false
otherwise.ClassCastException - when an object in the collection cannot be compared with the
elements in this TreeSet.NullPointerException - when an object in the collection is null and the comparator
cannot handle null.Collection.add(Object)public void clear()
TreeSet, leaving it empty.public Object clone()
TreeSet with the same elements, size and comparator
as this TreeSet.public Comparator<? super E> comparator()
TreeSet.comparator in interface SortedSet<E>public boolean contains(Object object)
TreeSet for the specified object.contains in interface Collection<E>contains in interface Set<E>contains in class AbstractCollection<E>object - the object to search for.true if object is an element of this
TreeSet, false otherwise.ClassCastException - when the object cannot be compared with the elements in this
TreeSet.NullPointerException - when the object is null and the comparator cannot handle
null.public boolean isEmpty()
TreeSet has no element, otherwise false.isEmpty in interface Collection<E>isEmpty in interface Set<E>isEmpty in class AbstractCollection<E>TreeSet has no element.size()public Iterator<E> iterator()
TreeSet.iterator in interface Iterable<E>iterator in interface Collection<E>iterator in interface NavigableSet<E>iterator in interface Set<E>iterator in class AbstractCollection<E>TreeSet.Iteratorpublic Iterator<E> descendingIterator()
descendingSet().iterator().descendingIterator in interface NavigableSet<E>NavigableSet.descendingIterator()public boolean remove(Object object)
TreeSet.remove in interface Collection<E>remove in interface Set<E>remove in class AbstractCollection<E>object - the object to remove.true if this TreeSet was modified, false
otherwise.ClassCastException - when the object cannot be compared with the elements in this
TreeSet.NullPointerException - when the object is null and the comparator cannot handle
null.public int size()
TreeSet.size in interface Collection<E>size in interface Set<E>size in class AbstractCollection<E>TreeSet.public E first()
first in interface SortedSet<E>NoSuchElementException - when this TreeSet is emptypublic E last()
last in interface SortedSet<E>NoSuchElementException - when this TreeSet is emptypublic E pollFirst()
null if this set is empty.pollFirst in interface NavigableSet<E>null if this set is emptyNavigableSet.pollFirst()public E pollLast()
null if this set is empty.pollLast in interface NavigableSet<E>null if this set is emptyNavigableSet.pollLast()public E higher(E e)
null if there is no such element.higher in interface NavigableSet<E>e - the value to matche,
or null if there is no such elementNavigableSet.higher(java.lang.Object)public E lower(E e)
null if there is no such element.lower in interface NavigableSet<E>e - the value to matche,
or null if there is no such elementNavigableSet.lower(java.lang.Object)public E ceiling(E e)
null if there is no such element.ceiling in interface NavigableSet<E>e - the value to matche,
or null if there is no such elementNavigableSet.ceiling(java.lang.Object)public E floor(E e)
null if there is no such element.floor in interface NavigableSet<E>e - the value to matche,
or null if there is no such elementNavigableSet.floor(java.lang.Object)public NavigableSet<E> descendingSet()
remove operation), the results of
the iteration are undefined.
The returned set has an ordering equivalent to
Collections.reverseOrder(comparator()).
The expression s.descendingSet().descendingSet() returns a
view of s essentially equivalent to s.
descendingSet in interface NavigableSet<E>NavigableSet.descendingSet()public NavigableSet<E> subSet(E start, boolean startInclusive, E end, boolean endInclusive)
fromElement to toElement. If fromElement and
toElement are equal, the returned set is empty unless fromInclusive and toInclusive are both true. The returned set
is backed by this set, so changes in the returned set are reflected in
this set, and vice-versa. The returned set supports all optional set
operations that this set supports.
The returned set will throw an IllegalArgumentException
on an attempt to insert an element outside its range.
subSet in interface NavigableSet<E>start - low endpoint of the returned setstartInclusive - true if the low endpoint
is to be included in the returned viewend - high endpoint of the returned setendInclusive - true if the high endpoint
is to be included in the returned viewfromElement, inclusive, to toElement, exclusiveNavigableSet.subSet(Object, boolean, Object, boolean)public NavigableSet<E> headSet(E end, boolean endInclusive)
inclusive is true) toElement. The
returned set is backed by this set, so changes in the returned set are
reflected in this set, and vice-versa. The returned set supports all
optional set operations that this set supports.
The returned set will throw an IllegalArgumentException
on an attempt to insert an element outside its range.
headSet in interface NavigableSet<E>end - high endpoint of the returned setendInclusive - true if the high endpoint
is to be included in the returned viewinclusive is true) toElementNavigableSet.headSet(Object, boolean)public NavigableSet<E> tailSet(E start, boolean startInclusive)
inclusive is true) fromElement.
The returned set is backed by this set, so changes in the returned set
are reflected in this set, and vice-versa. The returned set supports
all optional set operations that this set supports.
The returned set will throw an IllegalArgumentException
on an attempt to insert an element outside its range.
tailSet in interface NavigableSet<E>start - low endpoint of the returned setstartInclusive - true if the low endpoint
is to be included in the returned viewfromElementNavigableSet.tailSet(Object, boolean)public SortedSet<E> subSet(E start, E end)
SortedSet of the specified portion of this TreeSet which
contains elements greater or equal to the start element but less than the
end element. The returned SortedSet is backed by this TreeSet so changes
to one are reflected by the other.subSet in interface NavigableSet<E>subSet in interface SortedSet<E>start - the start elementend - the end elementstart and less than endClassCastException - when the start or end object cannot be compared with the
elements in this TreeSetNullPointerException - when the start or end object is null and the comparator
cannot handle nullpublic SortedSet<E> headSet(E end)
SortedSet of the specified portion of this TreeSet which
contains elements less than the end element. The returned SortedSet is
backed by this TreeSet so changes to one are reflected by the other.headSet in interface NavigableSet<E>headSet in interface SortedSet<E>end - the end elementendClassCastException - when the end object cannot be compared with the elements
in this TreeSetNullPointerException - when the end object is null and the comparator cannot
handle nullpublic SortedSet<E> tailSet(E start)
SortedSet of the specified portion of this TreeSet which
contains elements greater or equal to the start element. The returned
SortedSet is backed by this TreeSet so changes to one are reflected by
the other.tailSet in interface NavigableSet<E>tailSet in interface SortedSet<E>start - the start elementstartClassCastException - when the start object cannot be compared with the elements
in this TreeSetNullPointerException - when the start object is null and the comparator cannot
handle null