Class 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 of
TreeSetwhich uses natural ordering and containing the unique elements in the specified collection.Parameters
collection: the collection of elements to add.
Throws
ClassCastException: @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
TreeSetwhich uses the specified comparator.Parameters
comparator: the comparator to use.
-
TreeSet
-
-
Method Details
-
add
Adds the specified object to this
TreeSet.Parameters
object: the object to add.
Returns
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Returns:
truewhen thisTreeSetdid not already contain the object,falseotherwise.Throws
-
ClassCastException: @throws ClassCastException when the object cannot be compared with the elements in thisTreeSet. -
NullPointerException: @throws NullPointerException when the object is null and the comparator cannot handle null.
-
-
addAll
Adds the objects in the specified collection to this
TreeSet.Parameters
collection: the collection of objects to add.
Returns
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceSet<E>- Overrides:
addAllin classAbstractCollection<E>- Returns:
trueif thisTreeSetwas modified,falseotherwise.Throws
-
ClassCastException: @throws ClassCastException when an object in the collection cannot be compared with the elements in thisTreeSet. -
NullPointerException: @throws NullPointerException when an object in the collection is null and the comparator cannot handle null.
-
-
clear
public void clear()Removes all elements from this
TreeSet, leaving it empty.See also
-
#isEmpty
-
#size
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
-
comparator
Returns the comparator used to compare elements in this
TreeSet.Returns
a Comparator or null if the natural ordering is used
- Specified by:
comparatorin interfaceSortedSet<E>
-
contains
Searches this
TreeSetfor the specified object.Parameters
object: the object to search for.
Returns
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Returns:
trueifobjectis an element of thisTreeSet,falseotherwise.Throws
-
ClassCastException: @throws ClassCastException when the object cannot be compared with the elements in thisTreeSet. -
NullPointerException: @throws NullPointerException when the object is null and the comparator cannot handle null.
-
-
isEmpty
public boolean isEmpty()Returns true if this
TreeSethas no element, otherwise false.Returns
true if this
TreeSethas no element.See also
- #size
- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif thisCollectionhas no elements,falseotherwise.See also
- #size
-
iterator
Returns an Iterator on the elements of this
TreeSet.Returns
an Iterator on the elements of this
TreeSet.See also
- Iterator
-
descendingIterator
Answers a descending iterator of this set.
Returns
the descending iterator
Since
1.6
See also
- java.util.NavigableSet#descendingIterator()
- Specified by:
descendingIteratorin interfaceNavigableSet<E>
-
remove
Removes an occurrence of the specified object from this
TreeSet.Parameters
object: the object to remove.
Returns
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Returns:
trueif thisTreeSetwas modified,falseotherwise.Throws
-
ClassCastException: @throws ClassCastException when the object cannot be compared with the elements in thisTreeSet. -
NullPointerException: @throws NullPointerException when the object is null and the comparator cannot handle null.
-
-
size
public int size()Returns the number of elements in this
TreeSet.Returns
the number of elements in this
TreeSet.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
first
-
last
-
pollFirst
Deletes and answers the smallest element, or null if the set is empty.
Returns
the smallest element, or null if the set is empty
Since
1.6
See also
- java.util.NavigableSet#pollFirst()
- Specified by:
pollFirstin interfaceNavigableSet<E>
-
pollLast
Deletes and answers the biggest element, or null if the set is empty.
Returns
the biggest element, or null if the set is empty
Since
1.6
See also
- java.util.NavigableSet#pollLast()
- Specified by:
pollLastin interfaceNavigableSet<E>
-
higher
Answers the smallest element bigger than the specified one, or null if no such element.
Parameters
e: the specified element
Returns
Since
1.6
See also
- java.util.NavigableSet#higher(java.lang.Object)
- Specified by:
higherin interfaceNavigableSet<E>- Returns:
the smallest element bigger than the specified one, or null if no such element
Throws
-
ClassCastException: if the element cannot be compared with the ones in the set -
NullPointerException: if the element is null and the set can not contain null
-
-
lower
Answers the biggest element less than the specified one, or null if no such element.
Parameters
e: the specified element
Returns
Since
1.6
See also
- java.util.NavigableSet#lower(java.lang.Object)
- Specified by:
lowerin interfaceNavigableSet<E>- Returns:
the biggest element less than the specified one, or null if no such element
Throws
-
ClassCastException: if the element cannot be compared with the ones in the set -
NullPointerException: if the element is null and the set can not contain null
-
-
ceiling
Answers the smallest element bigger than or equal to the specified one, or null if no such element.
Parameters
e: the specified element
Returns
Since
1.6
See also
- java.util.NavigableSet#ceiling(java.lang.Object)
- Specified by:
ceilingin interfaceNavigableSet<E>- Returns:
the smallest element bigger than or equal to the specified one, or null if no such element
Throws
-
ClassCastException: if the element cannot be compared with the ones in the set -
NullPointerException: if the element is null and the set can not contain null
-
-
floor
Answers the biggest element less than or equal to the specified one, or null if no such element.
Parameters
e: the specified element
Returns
Since
1.6
See also
- java.util.NavigableSet#floor(java.lang.Object)
- Specified by:
floorin interfaceNavigableSet<E>- Returns:
the biggest element less than or equal to the specified one, or null if no such element
Throws
-
ClassCastException: if the element cannot be compared with the ones in the set -
NullPointerException: if the element is null and the set can not contain null
-
-
descendingSet
Answers a reverse order view of this set.
Returns
the reverse order view
Since
1.6
See also
- java.util.NavigableSet#descendingSet()
- Specified by:
descendingSetin interfaceNavigableSet<E>
-
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.
Parameters
-
start: the start element -
startInclusive: true if the start element is in the returned set -
end: the end element -
endInclusive: true if the end element is in the returned set
Returns
the subset
Throws
-
ClassCastException: @throws ClassCastException when the start or end object cannot be compared with the elements in this set -
NullPointerException: @throws NullPointerException when the start or end object is null and the set cannot contain null -
IllegalArgumentException: @throws IllegalArgumentException when the start is bigger than end; or start or end is out of range and the set has a range
Since
1.6
See also
- java.util.NavigableSet#subSet(Object, boolean, Object, boolean)
- Specified by:
subSetin interfaceNavigableSet<E>
-
-
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.
Parameters
-
end: the end element -
endInclusive: true if the end element is in the returned set
Returns
the subset
Throws
-
ClassCastException: @throws ClassCastException when the end object cannot be compared with the elements in this set -
NullPointerException: @throws NullPointerException when the end object is null and the set cannot contain handle null -
IllegalArgumentException: when end is out of range and the set has a range
Since
1.6
See also
- java.util.NavigableSet#headSet(Object, boolean)
- Specified by:
headSetin interfaceNavigableSet<E>
-
-
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.
Parameters
-
start: the start element -
startInclusive: true if the start element is in the returned set
Returns
the subset
Throws
-
ClassCastException: @throws ClassCastException when the start object cannot be compared with the elements in this set -
NullPointerException: when the start object is null and the set cannot contain null -
IllegalArgumentException: when start is out of range and the set has a range
Since
1.6
See also
- java.util.NavigableSet#tailSet(Object, boolean)
- Specified by:
tailSetin interfaceNavigableSet<E>
-
-
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.
Parameters
-
start: the start element -
end: the end element
Returns
- Specified by:
subSetin interfaceSortedSet<E>- Returns:
a subset where the elements are greater or equal to
startand less thanendThrows
-
ClassCastException: @exception ClassCastException when the start or end object cannot be compared with the elements in this TreeSet -
NullPointerException: @exception NullPointerException 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.
Parameters
end: the end element
Returns
a subset where the elements are less than
endThrows
-
ClassCastException: @exception ClassCastException when the end object cannot be compared with the elements in this TreeSet -
NullPointerException: @exception NullPointerException 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.
Parameters
start: the start element
Returns
- Specified by:
tailSetin interfaceSortedSet<E>- Returns:
a subset where the elements are greater or equal to
startThrows
-
ClassCastException: @exception ClassCastException when the start object cannot be compared with the elements in this TreeSet -
NullPointerException: @exception NullPointerException when the start object is null and the comparator cannot handle null
-
-