Package java.util
Class TreeSet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
java.util.TreeSet<E>
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,NavigableSet<E>,Set<E>,SortedSet<E>
public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable
TreeSet is an implementation of SortedSet. All optional operations (adding
and removing) are supported. The elements can be any objects which are
comparable to each other either using their natural order or a specified
Comparator.
- Since:
- 1.2
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description TreeSet()Constructs a new empty instance ofTreeSetwhich uses natural ordering.TreeSet(Collection<? extends E> collection)Constructs a new instance ofTreeSetwhich uses natural ordering and containing the unique elements in the specified collection.TreeSet(Comparator<? super E> comparator)Constructs a new empty instance ofTreeSetwhich uses the specified comparator.TreeSet(SortedSet<E> set)Constructs a new instance ofTreeSetcontaining the elements of the specified SortedSet and using the same Comparator. -
Method Summary
Modifier and Type Method Description booleanadd(E object)Adds the specified object to thisTreeSet.booleanaddAll(Collection<? extends E> collection)Adds the objects in the specified collection to thisTreeSet.Eceiling(E e)Returns the least element in this set greater than or equal to the given element, ornullif there is no such element.voidclear()Removes all elements from thisTreeSet, leaving it empty.Objectclone()Returns a newTreeSetwith the same elements, size and comparator as thisTreeSet.Comparator<? super E>comparator()Returns the comparator used to compare elements in thisTreeSet.booleancontains(Object object)Searches thisTreeSetfor 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.Efirst()Returns the first element in this set.Efloor(E e)Returns the greatest element in this set less than or equal to the given element, ornullif there is no such element.SortedSet<E>headSet(E end)Returns aSortedSetof the specified portion of thisTreeSetwhich 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, ifinclusiveis true)toElement.Ehigher(E e)Returns the least element in this set strictly greater than the given element, ornullif there is no such element.booleanisEmpty()Returns true if thisTreeSethas no element, otherwise false.Iterator<E>iterator()Returns an Iterator on the elements of thisTreeSet.Elast()Returns the last element in this set.Elower(E e)Returns the greatest element in this set strictly less than the given element, ornullif there is no such element.EpollFirst()Retrieves and removes the first (lowest) element, or returnsnullif this set is empty.EpollLast()Retrieves and removes the last (highest) element, or returnsnullif this set is empty.booleanremove(Object object)Removes an occurrence of the specified object from thisTreeSet.intsize()Returns the number of elements in thisTreeSet.NavigableSet<E>subSet(E start, boolean startInclusive, E end, boolean endInclusive)Returns a view of the portion of this set whose elements range fromfromElementtotoElement.SortedSet<E>subSet(E start, E end)Returns aSortedSetof the specified portion of thisTreeSetwhich contains elements greater or equal to the start element but less than the end element.SortedSet<E>tailSet(E start)Returns aSortedSetof the specified portion of thisTreeSetwhich 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, ifinclusiveis true)fromElement.Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray, toString
-
Constructor Details
-
TreeSet
public TreeSet()Constructs a new empty instance ofTreeSetwhich uses natural ordering. -
TreeSet
Constructs a new instance ofTreeSetwhich uses natural ordering and containing the unique elements in the specified collection.- Parameters:
collection- the collection of elements to add.- Throws:
ClassCastException- when an element in the collection does not implement the Comparable interface, or the elements in the collection cannot be compared.
-
TreeSet
Constructs a new empty instance ofTreeSetwhich uses the specified comparator.- Parameters:
comparator- the comparator to use.
-
TreeSet
Constructs a new instance ofTreeSetcontaining the elements of the specified SortedSet and using the same Comparator.- Parameters:
set- the SortedSet of elements to add.
-
-
Method Details
-
add
Adds the specified object to thisTreeSet.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
object- the object to add.- Returns:
truewhen thisTreeSetdid not already contain the object,falseotherwise.- Throws:
ClassCastException- when the object cannot be compared with the elements in thisTreeSet.NullPointerException- when the object is null and the comparator cannot handle null.
-
addAll
Adds the objects in the specified collection to thisTreeSet.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceSet<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
collection- the collection of objects to add.- Returns:
trueif thisTreeSetwas modified,falseotherwise.- Throws:
ClassCastException- when an object in the collection cannot be compared with the elements in thisTreeSet.NullPointerException- when an object in the collection is null and the comparator cannot handle null.
-
clear
public void clear()Removes all elements from thisTreeSet, leaving it empty. -
clone
Returns a newTreeSetwith the same elements, size and comparator as thisTreeSet. -
comparator
Returns the comparator used to compare elements in thisTreeSet.- Specified by:
comparatorin interfaceSortedSet<E>- Returns:
- a Comparator or null if the natural ordering is used
-
contains
Searches thisTreeSetfor the specified object.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
object- the object to search for.- Returns:
trueifobjectis an element of thisTreeSet,falseotherwise.- Throws:
ClassCastException- when the object cannot be compared with the elements in thisTreeSet.NullPointerException- when the object is null and the comparator cannot handle null.
-
isEmpty
public boolean isEmpty()Returns true if thisTreeSethas no element, otherwise false.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
- true if this
TreeSethas no element. - See Also:
size()
-
iterator
Returns an Iterator on the elements of thisTreeSet.- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin interfaceNavigableSet<E>- Specified by:
iteratorin interfaceSet<E>- Specified by:
iteratorin classAbstractCollection<E>- Returns:
- an Iterator on the elements of this
TreeSet. - See Also:
Iterator
-
descendingIterator
Returns an iterator over the elements in this set, in descending order. Equivalent in effect todescendingSet().iterator().- Specified by:
descendingIteratorin interfaceNavigableSet<E>- Returns:
- an iterator over the elements in this set, in descending order
- Since:
- 1.6
- See Also:
NavigableSet.descendingIterator()
-
remove
Removes an occurrence of the specified object from thisTreeSet.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
object- the object to remove.- Returns:
trueif thisTreeSetwas modified,falseotherwise.- Throws:
ClassCastException- when the object cannot be compared with the elements in thisTreeSet.NullPointerException- when the object is null and the comparator cannot handle null.
-
size
public int size()Returns the number of elements in thisTreeSet.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this
TreeSet.
-
first
Returns the first element in this set.- Specified by:
firstin interfaceSortedSet<E>- Returns:
- the first element.
- Throws:
NoSuchElementException- when this TreeSet is empty
-
last
Returns the last element in this set.- Specified by:
lastin interfaceSortedSet<E>- Returns:
- the last element.
- Throws:
NoSuchElementException- when this TreeSet is empty
-
pollFirst
Retrieves and removes the first (lowest) element, or returnsnullif this set is empty.- Specified by:
pollFirstin interfaceNavigableSet<E>- Returns:
- the first element, or
nullif this set is empty - Since:
- 1.6
- See Also:
NavigableSet.pollFirst()
-
pollLast
Retrieves and removes the last (highest) element, or returnsnullif this set is empty.- Specified by:
pollLastin interfaceNavigableSet<E>- Returns:
- the last element, or
nullif this set is empty - Since:
- 1.6
- See Also:
NavigableSet.pollLast()
-
higher
Returns the least element in this set strictly greater than the given element, ornullif there is no such element.- Specified by:
higherin interfaceNavigableSet<E>- Parameters:
e- the value to match- Returns:
- the least element greater than
e, ornullif there is no such element - Since:
- 1.6
- See Also:
NavigableSet.higher(java.lang.Object)
-
lower
Returns the greatest element in this set strictly less than the given element, ornullif there is no such element.- Specified by:
lowerin interfaceNavigableSet<E>- Parameters:
e- the value to match- Returns:
- the greatest element less than
e, ornullif there is no such element - Since:
- 1.6
- See Also:
NavigableSet.lower(java.lang.Object)
-
ceiling
Returns the least element in this set greater than or equal to the given element, ornullif there is no such element.- Specified by:
ceilingin interfaceNavigableSet<E>- Parameters:
e- the value to match- Returns:
- the least element greater than or equal to
e, ornullif there is no such element - Since:
- 1.6
- See Also:
NavigableSet.ceiling(java.lang.Object)
-
floor
Returns the greatest element in this set less than or equal to the given element, ornullif there is no such element.- Specified by:
floorin interfaceNavigableSet<E>- Parameters:
e- the value to match- Returns:
- the greatest element less than or equal to
e, ornullif there is no such element - Since:
- 1.6
- See Also:
NavigableSet.floor(java.lang.Object)
-
descendingSet
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 ownremoveoperation), the results of the iteration are undefined.The returned set has an ordering equivalent to
Collections.reverseOrder(comparator()). The expressions.descendingSet().descendingSet()returns a view ofsessentially equivalent tos.- Specified by:
descendingSetin interfaceNavigableSet<E>- Returns:
- a reverse order view of this set
- Since:
- 1.6
- See Also:
NavigableSet.descendingSet()
-
subSet
Returns a view of the portion of this set whose elements range fromfromElementtotoElement. IffromElementandtoElementare equal, the returned set is empty unlessfromExclusiveandtoExclusiveare 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
IllegalArgumentExceptionon an attempt to insert an element outside its range.- Specified by:
subSetin interfaceNavigableSet<E>- Parameters:
start- low endpoint of the returned setstartInclusive-trueif the low endpoint is to be included in the returned viewend- high endpoint of the returned setendInclusive-trueif the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this set whose elements range from
fromElement, inclusive, totoElement, exclusive - Since:
- 1.6
- See Also:
NavigableSet.subSet(Object, boolean, Object, boolean)
-
headSet
Returns a view of the portion of this set whose elements are less than (or equal to, ifinclusiveis 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
IllegalArgumentExceptionon an attempt to insert an element outside its range.- Specified by:
headSetin interfaceNavigableSet<E>- Parameters:
end- high endpoint of the returned setendInclusive-trueif the high endpoint is to be included in the returned view- Returns:
- a view of the portion of this set whose elements are less than
(or equal to, if
inclusiveis true)toElement - Since:
- 1.6
- See Also:
NavigableSet.headSet(Object, boolean)
-
tailSet
Returns a view of the portion of this set whose elements are greater than (or equal to, ifinclusiveis 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
IllegalArgumentExceptionon an attempt to insert an element outside its range.- Specified by:
tailSetin interfaceNavigableSet<E>- Parameters:
start- low endpoint of the returned setstartInclusive-trueif the low endpoint is to be included in the returned view- Returns:
- a view of the portion of this set whose elements are greater
than or equal to
fromElement - Since:
- 1.6
- See Also:
NavigableSet.tailSet(Object, boolean)
-
subSet
Returns aSortedSetof the specified portion of thisTreeSetwhich 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.- Specified by:
subSetin interfaceNavigableSet<E>- Specified by:
subSetin interfaceSortedSet<E>- Parameters:
start- the start elementend- the end element- Returns:
- a subset where the elements are greater or equal to
startand less thanend - Throws:
ClassCastException- 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 null
-
headSet
Returns aSortedSetof the specified portion of thisTreeSetwhich contains elements less than the end element. The returned SortedSet is backed by this TreeSet so changes to one are reflected by the other.- Specified by:
headSetin interfaceNavigableSet<E>- Specified by:
headSetin interfaceSortedSet<E>- Parameters:
end- the end element- Returns:
- a subset where the elements are less than
end - Throws:
ClassCastException- when the end object cannot be compared with the elements in this TreeSetNullPointerException- when the end object is null and the comparator cannot handle null
-
tailSet
Returns aSortedSetof the specified portion of thisTreeSetwhich 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.- Specified by:
tailSetin interfaceNavigableSet<E>- Specified by:
tailSetin interfaceSortedSet<E>- Parameters:
start- the start element- Returns:
- a subset where the elements are greater or equal to
start - Throws:
ClassCastException- 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
-