Final

java.util
Class TreeSet<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by java.util.TreeSet<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>, SortedSet<E>

public class TreeSet<E>
extends AbstractSet<E>
implements SortedSet<E>, Cloneable, Serializable

TreeSet is an implementation of SortedSet. 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.

See Also:
Serialized Form

Constructor Summary
TreeSet()
          Contructs 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)
          Contructs a new empty instance of TreeSet which uses the specified Comparator.
TreeSet(SortedSet<E> set)
          Constructs a new instance of TreeSet containing the elements in the specified SortedSet and using the same Comparator.
 
Method Summary
 boolean add(E object)
          Adds the specified object to this TreeSet.
 boolean addAll(Collection<? extends E> collection)
          Adds the objects in the specified Collection to this TreeSet.
 void clear()
          Removes all elements from this TreeSet, leaving it empty.
 Object clone()
          Answers a new TreeSet with the same elements, size and comparator as this TreeSet.
 Comparator<? super E> comparator()
          Answers the Comparator used to compare elements in this TreeSet.
 boolean contains(Object object)
          Searches this TreeSet for the specified object.
 E first()
          Answers the first element in this TreeSet.
 SortedSet<E> headSet(E end)
          Answers a SortedSet of the specified portion of this TreeSet which contains elements less than the end element.
 boolean isEmpty()
          Answers if this TreeSet has no elements, a size of zero.
 Iterator<E> iterator()
          Answers an Iterator on the elements of this TreeSet.
 E last()
          Answers the last element in this TreeSet.
 boolean remove(Object object)
          Removes an occurrence of the specified object from this TreeSet.
 int size()
          Answers the number of elements in this TreeSet.
 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.
 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.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
 

Constructor Detail

TreeSet

public TreeSet()
Contructs 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 - 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)
Contructs a new empty instance of TreeSet which uses the specified Comparator.

Parameters:
comparator - the Comparator

TreeSet

public TreeSet(SortedSet<E> set)
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 Detail

add

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

Specified by:
add in interface Collection<E>
Specified by:
add in interface Set<E>
Overrides:
add in class AbstractCollection<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 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.

Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface Set<E>
Overrides:
addAll in class AbstractCollection<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 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.

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface Set<E>
Overrides:
clear in class AbstractCollection<E>
See Also:
isEmpty(), size()

clone

public Object clone()
Answers a new TreeSet with the same elements, size and comparator as this TreeSet.

Overrides:
clone in class Object
Returns:
a shallow copy of this TreeSet
See Also:
Cloneable

comparator

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

Specified by:
comparator in interface SortedSet<E>
Returns:
a Comparator or null if the natural ordering is used

contains

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

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface Set<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
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

first

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

Specified by:
first in interface SortedSet<E>
Returns:
the first element
Throws:
NoSuchElementException - when this TreeSet is empty

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.

Specified by:
headSet in interface SortedSet<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 TreeSet
NullPointerException - when the end 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:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface Set<E>
Overrides:
isEmpty in class AbstractCollection<E>
Returns:
true if this TreeSet has no elements, false otherwise
See Also:
size()

iterator

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

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface Set<E>
Specified by:
iterator in class AbstractCollection<E>
Returns:
an Iterator on the elements of this TreeSet
See Also:
Iterator

last

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

Specified by:
last in interface SortedSet<E>
Returns:
the last element
Throws:
NoSuchElementException - when this TreeSet is empty

remove

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

Specified by:
remove in interface Collection<E>
Specified by:
remove in interface Set<E>
Overrides:
remove in class AbstractCollection<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 TreeSet
NullPointerException - when the object is null and the comparator cannot handle null

size

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

Specified by:
size in interface Collection<E>
Specified by:
size in interface Set<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the number of elements in this TreeSet

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.

Specified by:
subSet in interface SortedSet<E>
Parameters:
start - the start element
end - 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

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.

Specified by:
tailSet in interface SortedSet<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 TreeSet
NullPointerException - when the start object is null and the comparator cannot handle null

Final

Licensed Materials - Property of IBM
© Copyright IBM Corp. 2006, 2008 All Rights Reserved.