Name
TreeSet<E>
Synopsis
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access