java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
java.util.TreeSet<E>
- Type Parameters:
E- type of element
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,NavigableSet<E>,Set<E>,SortedSet<E>
TreeSet is an implementation of NavigableSet. All optional operations are
supported, adding and removing. 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:
-
Constructor Summary
ConstructorsConstructorDescriptionTreeSet()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.Constructs a new instance of TreeSet containing the elements in the specified SortedSet and using the same Comparator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the specified object to this TreeSet.booleanaddAll(Collection<? extends E> collection) Adds the objects in the specified Collection to this TreeSet.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 this TreeSet, leaving it empty.clone()Answers a new TreeSet with the same elements, size and comparator as this TreeSet.Comparator<? super E>Answers the Comparator used to compare elements in this TreeSet.booleanSearches this TreeSet for the specified object.Returns an iterator over the elements in this set, in descending order.Returns a reverse order view of the elements contained in this set.first()Answers the first element in this TreeSet.Returns the greatest element in this set less than or equal to the given element, ornullif there is no such element.Answers a SortedSet of the specified portion of this TreeSet which contains elements less than the end element.Returns a view of the portion of this set whose elements are less than (or equal to, ifinclusiveis true)toElement.Returns the least element in this set strictly greater than the given element, ornullif there is no such element.booleanisEmpty()Answers if this TreeSet has no elements, a size of zero.iterator()Answers an Iterator on the elements of this TreeSet.last()Answers the last element in this TreeSet.Returns the greatest element in this set strictly less than the given element, ornullif there is no such element.Retrieves and removes the first (lowest) element, or returnsnullif this set is empty.pollLast()Retrieves and removes the last (highest) element, or returnsnullif this set is empty.booleanRemoves an occurrence of the specified object from this TreeSet.intsize()Answers the number of elements in this TreeSet.Returns a view of the portion of this set whose elements range fromfromElementtotoElement.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.Returns a view of the portion of this set whose elements are greater than (or equal to, ifinclusiveis true)fromElement.Methods declared in class java.util.AbstractSet
equals, hashCode, removeAllMethods declared in class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray, toStringMethods declared in interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods declared in interface java.util.Set
containsAll, equals, hashCode, removeAll, retainAll, toArray, toArrayMethods declared in interface java.util.SortedSet
spliterator
-
Constructor Details
-
TreeSet
public TreeSet()Constructs a new empty instance of TreeSet which uses natural ordering. -
TreeSet
Constructs a new instance of TreeSet which 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 of TreeSet which uses the specified Comparator.- Parameters:
comparator- the Comparator
-
TreeSet
Constructs a new instance of TreeSet containing the elements in the specified SortedSet and using the same Comparator.- Parameters:
set- the SortedSet of elements to add
-
-
Method Details
-
add
Adds the specified object to this TreeSet.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
object- the object to add- Returns:
- true when this TreeSet did not already contain the object, false otherwise
- Throws:
ClassCastException- when the object cannot be compared with the elements in this TreeSetNullPointerException- when the object is null and the comparator cannot handle null
-
addAll
Adds the objects in the specified Collection to this TreeSet.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceSet<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
collection- the Collection of objects- Returns:
- true if this TreeSet is modified, false otherwise
- Throws:
ClassCastException- when an object in the Collection cannot be compared with the elements in this TreeSetNullPointerException- when an object in the Collection is null and the comparator cannot handle null- See Also:
-
clear
public void clear()Removes all elements from this TreeSet, leaving it empty. -
clone
Answers a new TreeSet with the same elements, size and comparator as this TreeSet. -
comparator
Answers the Comparator used to compare elements in this TreeSet.- Specified by:
comparatorin interfaceSortedSet<E>- Returns:
- a Comparator or null if the natural ordering is used
-
contains
Searches this TreeSet for 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:
- true if
objectis an element of this TreeSet, false otherwise - Throws:
ClassCastException- when the object cannot be compared with the elements in this TreeSetNullPointerException- when the object is null and the comparator cannot handle null
-
isEmpty
public boolean isEmpty()Answers if this TreeSet has no elements, a size of zero.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
- true if this TreeSet has no elements, false otherwise
- See Also:
-
iterator
Answers an Iterator on the elements of this TreeSet.- 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:
-
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:
-
remove
Removes an occurrence of the specified object from this TreeSet.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
object- the object to remove- Returns:
- true if this TreeSet is modified, false otherwise
- Throws:
ClassCastException- when the object cannot be compared with the elements in this TreeSetNullPointerException- when the object is null and the comparator cannot handle null
-
size
public int size()Answers 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
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
subSet
Returns a view of the portion of this set whose elements range fromfromElementtotoElement. IffromElementandtoElementare equal, the returned set is empty unlessfromInclusiveandtoInclusiveare 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:
-
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:
-
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:
-
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 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
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 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
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 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
-