[all packages] [package java.util] [class hierarchy] [index]

public class java.util.TreeSet<A>

(source file: TreeSet.java)
java.lang.Object
   |
   +----java.util.AbstractCollection<A>
           |
           +----java.util.AbstractSet<A>
                   |
                   +----java.util.TreeSet<A>

The pure class interface.
public class TreeSet<A>
  extends AbstractSet<A>
  implements SortedSet<A>, Cloneable, java.io.Serializable
This class implements the Set interface, backed by a TreeMap. This class guarantees that the Map will be in ascending key order, sorted according to the natural sort method for the key Class (see Comparable), or by the Comparator provided at TreeSet creation time, depending on which constructor is used. Note that this ordering must be total in order for the Tree to function properly. (A total ordering is an ordering for which a.compareTo(b)==0 implies that a.equals(b).)

This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

Note that this implementation is not synchronized. If multiple threads access a TreeSet concurrently, and at least one of the threads modifies the TreeSet, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the TreeSet. If no such object exists, the TreeSet should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the TreeSet:

	Set s = Collections.synchronizedSet(new TreeSet(...));
 

The Iterators returned by TreeSet's iterator method are fail-fast: if the HashSet is modified at any time after the Iterator is created, in any way except through the Iterator's own remove method, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

See also:
Collection, Set, HashSet, ArraySet, Comparable, Comparator, Collections.synchronizedSet, TreeMap

Constuctor Index

O TreeSet(Collection<A>)
Constructs a new TreeSet containing the elements in the specified Collection, sorted
O TreeSet(SortedSet<A>)
Constructs a new TreeSet containing the same elements as the given SortedSet, sorted
O TreeSet(Comparator<A>)
Constructs a new, empty TreeSet, sorted according to the given comparator
O TreeSet()
Constructs a new, empty TreeSet, sorted according to the elements' natural sort method

Methods

O add(A)
Adds the specified element to this Set if it is not already present.
O clear()
Removes all of the elements from this Set.
O clone()
Returns a shallow copy of this TreeSet
O comparator()
Returns the comparator used to order this TreeMap, or null if this TreeMap uses its keys
O contains(A)
Returns true if this TreeSet contains the specified element.
O first()
Returns the first (lowest) element currently in this TreeSet.
O headSet(A)
Returns a view of the portion of this TreeSet whose elements are strictly less than
O isEmpty()
Returns true if this TreeSet contains no elements.
O iterator()
Returns an Iterator over the elements in this TreeSet
O last()
Returns the last (highest) element currently in this TreeSet.
O remove(A)
Removes the given element from this Set if it is present.
O size()
Returns the number of elements in this TreeSet (its cardinality).
O subSet(A, A)
Returns a view of the portion of this TreeSet whose elements range from fromElement,
O tailSet(A)
Returns a view of the portion of this TreeSet whose elements are strictly less than

Constructors

O TreeSet
public TreeSet();
Constructs a new, empty TreeSet, sorted according to the elements' natural sort method. All elements inserted into the TreeSet must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a typeMismatchException for any elements e1 and e2 in the TreeSet. If the user attempts to add an element to the TreeSet that violates this constraint (for example, the user attempts to add a String element to a TreeSet whose elements are Integers), the add(Object) call will throw a ClassCastException.

See also:
Comparable

O TreeSet

public TreeSet(Comparator<A> c);
Constructs a new, empty TreeSet, sorted according to the given comparator. All elements inserted into the TreeSet must be mutually comparable by the given comparator: comparator.compare(e1, e2) must not throw a typeMismatchException for any elements e1 and e2 in the TreeSet. If the user attempts to add an element to the TreeSet that violates this constraint, the add(Object) call will throw a ClassCastException.

O TreeSet

public TreeSet(Collection<A> c);
Constructs a new TreeSet containing the elements in the specified Collection, sorted according to the elements' natural sort method. All keys inserted into the TreeSet must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a typeMismatchException for any elements k1 and k2 in the TreeSet.

Throws:
ClassCastException -the keys in t are not Comparable, or are not mutually comparable.

O TreeSet

public TreeSet(SortedSet<A> s);
Constructs a new TreeSet containing the same elements as the given SortedSet, sorted according to the same ordering.

Methods

O iterator
public Iterator<A> iterator();
Returns an Iterator over the elements in this TreeSet. The elements are returned in ascending order.

Overrides:
iterator in class AbstractCollection

O size

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

Overrides:
size in class AbstractCollection

O isEmpty

public boolean isEmpty();
Returns true if this TreeSet contains no elements.

Overrides:
isEmpty in class AbstractCollection

O contains

public boolean contains(A o);
Returns true if this TreeSet contains the specified element.

Overrides:
contains in class AbstractCollection

O add

public boolean add(A o);
Adds the specified element to this Set if it is not already present.

Parameters:
o - element to be added to this Set.
Returns:
true if the Set did not already contain the specified element.
Overrides:
add in class AbstractCollection

O remove

public boolean remove(A o);
Removes the given element from this Set if it is present.

Parameters:
o - object to be removed from this Set, if present.
Returns:
true if the Set contained the specified element.
Overrides:
remove in class AbstractCollection

O clear

public void clear();
Removes all of the elements from this Set.

Overrides:
clear in class AbstractCollection

O subSet

public SortedSet<A> subSet(A fromElement,
                           A toElement);
Returns a view of the portion of this TreeSet whose elements range from fromElement, inclusive, to toElement, exclusive. The returned Set is backed by this TreeSet, so changes in the returned Set are reflected in this TreeSet, and vice-versa. The returned Set supports all optional Set operations. It does not support the subSet operation.

The Set returned by this method will throw an IllegalArgumentException if the user attempts to insert an element outside the specified range.

Parameters:
fromElement - low endpoint (inclusive) of the subSet.
fromElement - high endpoint (exclusive) of the subSet.
Throws:
ClassCastException -fromElement or toElement cannot be compared with the elements currently in the TreeSet.
NullPointerException -fromElement or toElement is null and this TreeSet uses natural sort method, or its comparator does not tolerate null elements.
IllegalArgumentException -fromElement is greater than toElement.

O headSet

public SortedSet<A> headSet(A toElement);
Returns a view of the portion of this TreeSet whose elements are strictly less than toElement. The returned Set is backed by this TreeSet, so changes in the returned Set are reflected in this TreeSet, and vice-versa. The returned Set supports all optional Set operations. It does not support the subSet operation, as it is not a TreeSet.

The Set returned by this method will throw an IllegalArgumentException if the user attempts to insert an element greater than or equal to toElement.

Parameters:
toElement - high endpoint (exclusive) of the headSet.
Throws:
ClassCastException -toElement cannot be compared with the elements currently in the TreeSet.
NullPointerException -toElement is null and this TreeSet uses natural sort method, or its comparator does not tolerate null elements.

O tailSet

public SortedSet<A> tailSet(A fromElement);
Returns a view of the portion of this TreeSet whose elements are strictly less than toElement. The returned Set is backed by this TreeSet, so changes in the returned Set are reflected in this TreeSet, and vice-versa. The returned Set supports all optional Set operations. It does not support the subSet operation, as it is not a TreeSet.

The Set returned by this method will throw an IllegalArgumentException if the user attempts to insert an element greater than or equal to toElement.

Parameters:
toElement - high endpoint (exclusive) of the headSet.
Throws:
ClassCastException -toElement cannot be compared with the elements currently in the TreeSet.
NullPointerException -toElement is null and this TreeSet uses natural sort method, or its comparator does not tolerate null elements.

O comparator

public Comparator<A> comparator();
Returns the comparator used to order this TreeMap, or null if this TreeMap uses its keys natural sort method.

O first

public A first();
Returns the first (lowest) element currently in this TreeSet.

Returns:
the first (lowest) element currently in this TreeSet.
Throws:
NoSuchElementException -Set is empty.

O last

public A last();
Returns the last (highest) element currently in this TreeSet.

Returns:
the last (highest) element currently in this TreeSet.
Throws:
NoSuchElementException -Set is empty.

O clone

public Object clone();
Returns a shallow copy of this TreeSet. (The elements themselves are not cloned.)

Overrides:
clone in class Object


[all packages] [package java.util] [class hierarchy] [index]
java.util.TreeSet.html