This class implements SortedSet , provides support for all optional methods , and guarantees that the elements of the set can be enumerated in ascending order. In order to be sorted, the elements of the set must all be mutually Comparable objects, or they must all be compatible with a Comparator object that is specified when the treeSet is created. TReeSet is implemented on top of a treeMap , so its add( ) , remove( ) , and contains( ) methods all operate in relatively efficient logarithmic time. If you do not need the sorting capability of treeSet , however, use HashSet instead, as it is significantly more efficient. See Set , SortedSet , and Collection for details on the methods of TReeSet . In order for a treeSet to operate correctly, the Comparable or Comparator comparison method must be consistent with the equals( ) method. That is, the equals( ) method must compare two objects as equal if and only if the comparison method also indicates those two objects are equal. The methods of TReeSet are not synchronized . If you are working in a multithreaded environment, you must explicitly synchronize code that modifies the contents of the set, or obtain a synchronized wrapper with Collections.synchronizedSet( ) . Figure 16-64. java.util.TreeSet<E>public class TreeSet<E> extends AbstractSet<E> implements SortedSet<E>, Cloneable, Serializable { // Public Constructors public TreeSet ( ); public TreeSet (Comparator<? super E> c ); public TreeSet (SortedSet<E> s ); public TreeSet (Collection<? extends E> c ); // Methods Implementing Set public boolean add (E o ); public boolean addAll (Collection<? extends E> c ); public void clear ( ); public boolean contains (Object o ); public boolean isEmpty ( ); default:true public Iterator<E> iterator ( ); public boolean remove (Object o ); public int size ( ); // Methods Implementing SortedSet public Comparator<? super E> comparator ( ); public E first ( ); public SortedSet<E> headSet (E toElement ); public E last ( ); public SortedSet<E> subSet (E fromElement , E toElement ); public SortedSet<E> tailSet (E fromElement ); // Public Methods Overriding Object public Object clone ( ); } |