Class TreeSet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
java.util.TreeSet<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>, NavigableSet<E>, Set<E>, SortedSet<E>
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
-
Constructor Summary
ConstructorsConstructorDescriptionTreeSet()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.Constructs a new instance ofTreeSetcontaining the elements of the specified SortedSet and using the same Comparator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the specified object to thisTreeSet.booleanaddAll(Collection<? extends E> collection) Adds the objects in the specified collection to thisTreeSet.Answers the smallest element bigger than or equal to the specified one, or null if no such element.voidclear()Removes all elements from thisTreeSet, leaving it empty.Comparator<? super E> Returns the comparator used to compare elements in thisTreeSet.booleanSearches thisTreeSetfor the specified object.Answers a descending iterator of this set.Answers a reverse order view of this set.first()Answers the first element in this TreeSet.Answers the biggest element less than or equal to the specified one, or null if no such element.Answers a SortedSet of the specified portion of this TreeSet which contains elements less than the end element.Answers a NavigableSet of the specified portion of this set which contains elements less than (or equal to, depends on endInclusive) the end element.Answers the smallest element bigger than the specified one, or null if no such element.booleanisEmpty()Returns true if thisTreeSethas no element, otherwise false.iterator()Returns an Iterator on the elements of thisTreeSet.last()Answers the last element in this TreeSet.Answers the biggest element less than the specified one, or null if no such element.Deletes and answers the smallest element, or null if the set is empty.pollLast()Deletes and answers the biggest element, or null if the set is empty.booleanRemoves an occurrence of the specified object from thisTreeSet.intsize()Returns the number of elements in thisTreeSet.Answers a NavigableSet of the specified portion of this set which contains elements greater (or equal to, depends on startInclusive) the start element but less than (or equal to, depends on endInclusive) the end element.Answers 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.Answers a SortedSet of the specified portion of this TreeSet which contains elements greater or equal to the start element.Answers a NavigableSet of the specified portion of this set which contains elements greater (or equal to, depends on startInclusive) the start element.Methods inherited from class AbstractSet
equals, hashCode, removeAllMethods inherited from class 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
-
-
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.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>- See Also:
-
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:
-
iterator
Returns an Iterator on the elements of thisTreeSet. -
descendingIterator
Answers a descending iterator of this set.- Specified by:
descendingIteratorin interfaceNavigableSet<E>- Returns:
- the descending iterator
- Since:
- 1.6
- See Also:
-
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
Answers the first element in this TreeSet.- Specified by:
firstin interfaceSortedSet<E>- Returns:
- the first element
- Throws:
NoSuchElementException- when this TreeSet is empty
-
last
Answers the last element in this TreeSet.- Specified by:
lastin interfaceSortedSet<E>- Returns:
- the last element
- Throws:
NoSuchElementException- when this TreeSet is empty
-
pollFirst
Deletes and answers the smallest element, or null if the set is empty.- Specified by:
pollFirstin interfaceNavigableSet<E>- Returns:
- the smallest element, or null if the set is empty
- Since:
- 1.6
- See Also:
-
pollLast
Deletes and answers the biggest element, or null if the set is empty.- Specified by:
pollLastin interfaceNavigableSet<E>- Returns:
- the biggest element, or null if the set is empty
- Since:
- 1.6
- See Also:
-
higher
Answers the smallest element bigger than the specified one, or null if no such element.- Specified by:
higherin interfaceNavigableSet<E>- Parameters:
e- the specified element- Returns:
- the smallest element bigger than the specified one, or null if no such element
- Since:
- 1.6
- See Also:
-
lower
Answers the biggest element less than the specified one, or null if no such element.- Specified by:
lowerin interfaceNavigableSet<E>- Parameters:
e- the specified element- Returns:
- the biggest element less than the specified one, or null if no such element
- Since:
- 1.6
- See Also:
-
ceiling
Answers the smallest element bigger than or equal to the specified one, or null if no such element.- Specified by:
ceilingin interfaceNavigableSet<E>- Parameters:
e- the specified element- Returns:
- the smallest element bigger than or equal to the specified one, or null if no such element
- Since:
- 1.6
- See Also:
-
floor
Answers the biggest element less than or equal to the specified one, or null if no such element.- Specified by:
floorin interfaceNavigableSet<E>- Parameters:
e- the specified element- Returns:
- the biggest element less than or equal to the specified one, or null if no such element
- Since:
- 1.6
- See Also:
-
descendingSet
Answers a reverse order view of this set.- Specified by:
descendingSetin interfaceNavigableSet<E>- Returns:
- the reverse order view
- Since:
- 1.6
- See Also:
-
subSet
Answers a NavigableSet of the specified portion of this set which contains elements greater (or equal to, depends on startInclusive) the start element but less than (or equal to, depends on endInclusive) the end element. The returned NavigableSet is backed by this set so changes to one are reflected by the other.- Specified by:
subSetin interfaceNavigableSet<E>- Parameters:
start- the start elementstartInclusive- true if the start element is in the returned setend- the end elementendInclusive- true if the end element is in the returned set- Returns:
- the subset
- Since:
- 1.6
- See Also:
-
headSet
Answers a NavigableSet of the specified portion of this set which contains elements less than (or equal to, depends on endInclusive) the end element. The returned NavigableSet is backed by this set so changes to one are reflected by the other.- Specified by:
headSetin interfaceNavigableSet<E>- Parameters:
end- the end elementendInclusive- true if the end element is in the returned set- Returns:
- the subset
- Since:
- 1.6
- See Also:
-
tailSet
Answers a NavigableSet of the specified portion of this set which contains elements greater (or equal to, depends on startInclusive) the start element. The returned NavigableSet is backed by this set so changes to one are reflected by the other.- Specified by:
tailSetin interfaceNavigableSet<E>- Parameters:
start- the start elementstartInclusive- true if the start element is in the returned set- Returns:
- the subset
- Since:
- 1.6
- See Also:
-
subSet
Answers 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. The returned SortedSet is backed by this TreeSet so changes to one are reflected by the other.- 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
Answers a 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.- 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
Answers a 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.- 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
-