TreeSet

TreeSet

  •  It is a type of set which implement navigable set,which is sub interface of sortSet
  • It is implemented based on balanced tree data structure.
  • It does not allow duplicate object.but null insertion is allowed.
  • Does not preserve insertion  order,object inserted according to sorting order
  • It allows only comparable type object.
  • By default object are sorted in ascending order.

TreeSet overload constructor

— TreeSet(comparator c1)
— TreeSet(sortedSet c1)
— TreeSet(collection c1)

Example:

import java.util.*;

class TestSet{ 
public static void main(String args[]){

TreeSet<String> ts = new TreeSet<String>();
    ts.add("AJAY");
    ts.add("KUNDAN");
    ts.add("SUMAN");
    ts.add("RAJAT");

Iterator<String> itr=ts.iterator();
   while(itr.hasNext()){
       System.out.println(itr.next());
                       }
            }
}
----------------------------------------------------
Output : AJAY
         KUNDAN
         RAJAT
         SUMAN

Leave a Reply