The TreeSet Class in java

TreeSet extends AbstractSet and implements the NavigableSet interface. It creates a collection that uses a tree for storage. Objects are stored in sorted, ascending order. Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly.

TreeSet is a generic class that has this declaration:

class TreeSet<E>

Here, specifies the type of objects that the set will hold.

TreeSet has the following constructors:

TreeSet( )

TreeSet(Collection<? extends E> c)

TreeSet(Comparator<? super E> comp)

TreeSet(SortedSet<E> ss)

Demonstrate TreeSet

import java.util.*;

class TreeSetDemo {

public static void main(String args[]) { // Create a tree set.

TreeSet<String> ts = new TreeSet<String>();

//Add elements to the tree set. ts.add("C");









The output from this program is shown here:

[A, B, C, D, E, F]

Leave a Comment