weiss.util
Class TreeSet<AnyType>

java.lang.Object
  extended by weiss.util.AbstractCollection<AnyType>
      extended by weiss.util.TreeSet<AnyType>
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<AnyType>, Collection<AnyType>, Set<AnyType>, SortedSet<AnyType>

public class TreeSet<AnyType>
extends AbstractCollection<AnyType>
implements SortedSet<AnyType>

Balanced search tree implementation of SortedSet. Matches are based on comparator or compareTo.

See Also:
Serialized Form

Constructor Summary
TreeSet()
          Construct an empty TreeSet.
TreeSet(Collection<? extends AnyType> other)
          Construct a TreeSet from any collection.
TreeSet(Comparator<? super AnyType> c)
          Construct an empty TreeSet with a specified comparator.
TreeSet(SortedSet<AnyType> other)
          Construct a TreeSet from another SortedSet.
 
Method Summary
 boolean add(AnyType x)
          Adds an item to this collection.
 void clear()
          Change the size of this collection to zero.
 Comparator<? super AnyType> comparator()
          Return the comparator used by this TreeSet.
 boolean contains(java.lang.Object x)
          Tests if some item is in this collection.
 AnyType first()
          Find the smallest item in the set.
 AnyType getMatch(AnyType x)
          This method is not part of standard Java.
 Iterator<AnyType> iterator()
          Obtains an Iterator object used to traverse the collection.
 AnyType last()
          Find the largest item in the set.
 boolean remove(java.lang.Object x)
          Removes an item from this collection.
 int size()
          Returns the number of items in this collection.
 
Methods inherited from class weiss.util.AbstractCollection
equals, hashCode, isEmpty, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface weiss.util.Collection
isEmpty, toArray, toArray
 

Constructor Detail

TreeSet

public TreeSet()
Construct an empty TreeSet.


TreeSet

public TreeSet(Comparator<? super AnyType> c)
Construct an empty TreeSet with a specified comparator.


TreeSet

public TreeSet(SortedSet<AnyType> other)
Construct a TreeSet from another SortedSet.


TreeSet

public TreeSet(Collection<? extends AnyType> other)
Construct a TreeSet from any collection. Uses an O( N log N ) algorithm, but could be improved.

Method Detail

comparator

public Comparator<? super AnyType> comparator()
Return the comparator used by this TreeSet.

Specified by:
comparator in interface SortedSet<AnyType>
Returns:
the comparator or null if the default comparator is used.

size

public int size()
Returns the number of items in this collection.

Specified by:
size in interface Collection<AnyType>
Returns:
the number of items in this collection.

first

public AnyType first()
Find the smallest item in the set.

Specified by:
first in interface SortedSet<AnyType>
Returns:
the smallest item.
Throws:
NoSuchElementException - if the set is empty.

last

public AnyType last()
Find the largest item in the set.

Specified by:
last in interface SortedSet<AnyType>
Returns:
the largest item.
Throws:
NoSuchElementException - if the set is empty.

getMatch

public AnyType getMatch(AnyType x)
This method is not part of standard Java. Like contains, it checks if x is in the set. If it is, it returns the reference to the matching object; otherwise it returns null.

Specified by:
getMatch in interface Set<AnyType>
Parameters:
x - the object to search for.
Returns:
if contains(x) is false, the return value is null; otherwise, the return value is the object that causes contains(x) to return true.

contains

public boolean contains(java.lang.Object x)
Tests if some item is in this collection.

Specified by:
contains in interface Collection<AnyType>
Overrides:
contains in class AbstractCollection<AnyType>
Parameters:
x - any object.
Returns:
true if this collection contains an item equal to x.

add

public boolean add(AnyType x)
Adds an item to this collection.

Specified by:
add in interface Collection<AnyType>
Overrides:
add in class AbstractCollection<AnyType>
Parameters:
x - any object.
Returns:
true if this item was added to the collection.

remove

public boolean remove(java.lang.Object x)
Removes an item from this collection.

Specified by:
remove in interface Collection<AnyType>
Overrides:
remove in class AbstractCollection<AnyType>
Parameters:
x - any object.
Returns:
true if this item was removed from the collection.

clear

public void clear()
Change the size of this collection to zero.

Specified by:
clear in interface Collection<AnyType>
Overrides:
clear in class AbstractCollection<AnyType>

iterator

public Iterator<AnyType> iterator()
Obtains an Iterator object used to traverse the collection.

Specified by:
iterator in interface java.lang.Iterable<AnyType>
Specified by:
iterator in interface Collection<AnyType>
Returns:
an iterator positioned prior to the first element.