Java - The TreeSet Class

TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in a sorted and 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.
Following is the list of the constructors supported by the TreeSet class.
TreeSet( )
This constructor constructs an empty tree set that will be sorted in an ascending order according to the natural order of its elements.
TreeSet(Collection c)
This constructor builds a tree set that contains the elements of the collection c.
TreeSet(Comparator comp) 
This constructor constructs an empty tree set that will be sorted according to the given comparator.
TreeSet(SortedSet ss)
This constructor builds a TreeSet that contains the elements of the given SortedSet.
Apart from the methods inherited from its parent classes, TreeSet defines the following methods −
void add(Object o)
Adds the specified element to this set if it is not already present.
boolean addAll(Collection c)
Adds all of the elements in the specified collection to this set.
void clear()
Removes all of the elements from this set.
Object clone()
Returns a shallow copy of this TreeSet instance.
Comparator comparator()
Returns the comparator used to order this sorted set, or null if this tree set uses its elements natural ordering.
boolean contains(Object o)
Returns true if this set contains the specified element.
Object first()
Returns the first (lowest) element currently in this sorted set.
SortedSet headSet(Object toElement)
Returns a view of the portion of this set whose elements are strictly less than toElement.
boolean isEmpty()
Returns true if this set contains no elements.
Iterator iterator()
Returns an iterator over the elements in this set.
Object last()
Returns the last (highest) element currently in this sorted set.
boolean remove(Object o)
Removes the specified element from this set if it is present.
int size()
Returns the number of elements in this set (its cardinality).
SortedSet subSet(Object fromElement, Object toElement)
Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
SortedSet tailSet(Object fromElement)
Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
The following program illustrates several of the methods supported by this collection −
import java.util.*;
public class TreeSetDemo {

   public static void main(String args[]) {
      // Create a tree set
      TreeSet ts = new TreeSet();
     
      // Add elements to the tree set
      ts.add("C");
      ts.add("A");
      ts.add("B");
      ts.add("E");
      ts.add("F");
      ts.add("D");
      System.out.println(ts);
   }
}
This will produce the following result −
[A, B, C, D, E, F]


    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page