Java TreeSet

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 has the following constructors:

TreeSet( )
TreeSet(Collection c)

The first form constructs an empty tree set that will be sorted in ascending order according to the natural order of its elements. The second form builds a tree set that contains the elements of c.

Progarm

0

 

 

 

Program Source

import java.util.TreeSet;

public class Javaapp {

    public static void main(String[] args) {
        
        TreeSet ts = new TreeSet();
        ts.add("BBB");
        ts.add("DDD");
        ts.add("CCC");
        ts.add("EEE");
        ts.add("AAA");
        System.out.println("ts : "+ts);
        System.out.println("ts size : "+ts.size());
        ts.remove("AAA");
        ts.remove("EEE");
        System.out.println("ts : "+ts);
        System.out.println("ts size : "+ts.size());
    }
}

Leave a Comment