public class TreeSet<E>

  1. Object
  2. AbstractCollection<E>
  3. AbstractSet<E>
  4. TreeSet

ImplementsCollection<E>, Iterable<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.

Constructors

public TreeSet()Constructs a new empty instance of TreeSet which uses natural ordering.
public TreeSet(Collection<? extends E> collection)Constructs a new instance of TreeSet which uses natural ordering and containing the unique elements in the specified collection.
public TreeSet(Comparator<? super E> comparator)Constructs a new empty instance of TreeSet which uses the specified comparator.
public TreeSet(SortedSet<E> set)Constructs a new instance of TreeSet containing the elements of the specified SortedSet and using the same Comparator.

Methods

public boolean add(E object)Adds the specified object to this TreeSet.
public boolean addAll(Collection<? extends E> collection)Adds the objects in the specified collection to this TreeSet.
public void clear()Removes all elements from this TreeSet, leaving it empty.
public Comparator<? super E> comparator()Returns the comparator used to compare elements in this TreeSet.
public boolean contains(Object object)Searches this TreeSet for the specified object.
public boolean isEmpty()Returns true if this TreeSet has no element, otherwise false.
public Iterator<E> iterator()Returns an Iterator on the elements of this TreeSet.
public Iterator<E> descendingIterator()Answers a descending iterator of this set.
public boolean remove(Object object)Removes an occurrence of the specified object from this TreeSet.
public int size()Returns the number of elements in this TreeSet.
public E first()Answers the first element in this TreeSet.
public E last()Answers the last element in this TreeSet.
public E pollFirst()Deletes and answers the smallest element, or null if the set is empty.
public E pollLast()Deletes and answers the biggest element, or null if the set is empty.
public E higher(E e)Answers the smallest element bigger than the specified one, or null if no such element.
public E lower(E e)Answers the biggest element less than the specified one, or null if no such element.
public E ceiling(E e)Answers the smallest element bigger than or equal to the specified one, or null if no such element.
public E floor(E e)Answers the biggest element less than or equal to the specified one, or null if no such element.
public NavigableSet<E> descendingSet()Answers a reverse order view of this set.
public NavigableSet<E> subSet(E start, boolean startInclusive, E end, boolean endInclusive)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.
public NavigableSet<E> headSet(E end, boolean endInclusive)Answers a NavigableSet of the specified portion of this set which contains elements less than (or equal to, depends on endInclusive) the end element.
public NavigableSet<E> tailSet(E start, boolean startInclusive)Answers a NavigableSet of the specified portion of this set which contains elements greater (or equal to, depends on startInclusive) the start element.
public SortedSet<E> subSet(E start, E end)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.
public SortedSet<E> headSet(E end)Answers a SortedSet of the specified portion of this TreeSet which contains elements less than the end element.
public SortedSet<E> tailSet(E start)Answers a SortedSet of the specified portion of this TreeSet which contains elements greater or equal to the start element.

Inherited methods

Constructor details

TreeSet

public TreeSet()
Constructs a new empty instance of TreeSet which uses natural ordering.

TreeSet

public TreeSet(Collection<? extends E> collection)
Constructs a new instance of TreeSet which uses natural ordering and containing the unique elements in the specified collection.

Parameters

collection Collection<? extends E>
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

public TreeSet(Comparator<? super E> comparator)
Constructs a new empty instance of TreeSet which uses the specified comparator.

Parameters

comparator Comparator<? super E>
the comparator to use.

TreeSet

public TreeSet(SortedSet<E> set)
Constructs a new instance of TreeSet containing the elements of the specified SortedSet and using the same Comparator.

Parameters

set SortedSet<E>
the SortedSet of elements to add.

Method details

add

public boolean add(E object)
Adds the specified object to this TreeSet.

Parameters

object E
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 TreeSet.
NullPointerException
when the object is null and the comparator cannot handle null.

addAll

public boolean addAll(Collection<? extends E> collection)
Adds the objects in the specified collection to this TreeSet.

Parameters

collection Collection<? extends E>
the collection of objects to add.

Returns

true if this TreeSet was modified, false otherwise.

Throws

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.

clear

public void clear()
Removes all elements from this TreeSet, leaving it empty.

Throws

UnsupportedOperationException
it the iterator does not support removing elements from this Collection

comparator

public Comparator<? super E> comparator()
Returns the comparator used to compare elements in this TreeSet.

Returns

a Comparator or null if the natural ordering is used

contains

public boolean contains(Object object)
Searches this TreeSet for the specified object.

Parameters

object Object
the object to search for.

Returns

true if object is an element of this TreeSet, false otherwise.

Throws

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.

isEmpty

public boolean isEmpty()
Returns true if this TreeSet has no element, otherwise false.

Returns

true if this TreeSet has no element.

See also

iterator

public Iterator<E> iterator()
Returns an Iterator on the elements of this TreeSet.

Returns

an Iterator on the elements of this TreeSet.

See also

descendingIterator

public Iterator<E> descendingIterator()
Answers a descending iterator of this set.

Returns

the descending iterator

remove

public boolean remove(Object object)
Removes an occurrence of the specified object from this TreeSet.

Parameters

object Object
the object to remove.

Returns

true if this TreeSet was modified, false otherwise.

Throws

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.

size

public int size()
Returns the number of elements in this TreeSet.

Returns

the number of elements in this TreeSet.

first

public E first()
Answers the first element in this TreeSet.

Returns

the first element

Throws

NoSuchElementException
when this TreeSet is empty

last

public E last()
Answers the last element in this TreeSet.

Returns

the last element

Throws

NoSuchElementException
when this TreeSet is empty

pollFirst

public E 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

pollLast

public 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

higher

public E higher(E e)
Answers the smallest element bigger than the specified one, or null if no such element.

Parameters

e E
the specified element

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

public E lower(E e)
Answers the biggest element less than the specified one, or null if no such element.

Parameters

e E
the specified element

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

public E ceiling(E e)
Answers the smallest element bigger than or equal to the specified one, or null if no such element.

Parameters

e E
the specified element

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

public E floor(E e)
Answers the biggest element less than or equal to the specified one, or null if no such element.

Parameters

e E
the specified element

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

public NavigableSet<E> descendingSet()
Answers a reverse order view of this set.

Returns

the reverse order view

subSet

public NavigableSet<E> subSet(E start, boolean startInclusive, E end, boolean endInclusive)
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 E
the start element
startInclusive boolean
true if the start element is in the returned set
end E
the end element
endInclusive boolean
true if the end element is in the returned set

Returns

the subset

Throws

ClassCastException
when the start or end object cannot be compared with the elements in this set
NullPointerException
when the start or end object is null and the set cannot contain null
IllegalArgumentException
when the start is bigger than end; or start or end is out of range and the set has a range

headSet

public NavigableSet<E> headSet(E end, boolean endInclusive)
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 E
the end element
endInclusive boolean
true if the end element is in the returned set

Returns

the subset

Throws

ClassCastException
when the end object cannot be compared with the elements in this set
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

tailSet

public NavigableSet<E> tailSet(E start, boolean startInclusive)
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 E
the start element
startInclusive boolean
true if the start element is in the returned set

Returns

the subset

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

subSet

public SortedSet<E> subSet(E start, E end)
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 E
the start element
end E
the end element

Returns

a subset where the elements are greater or equal to start and less than end

Throws

ClassCastException
when the start or end object cannot be compared with the elements in this TreeSet
NullPointerException
when the start or end object is null and the comparator cannot handle null

headSet

public SortedSet<E> headSet(E end)
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 E
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 TreeSet
NullPointerException
when the end object is null and the comparator cannot handle null

tailSet

public SortedSet<E> tailSet(E start)
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 E
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 TreeSet
NullPointerException
when the start object is null and the comparator cannot handle null