Untitled Document

Subscribe here to get updates on new Videos

 

32. TreeSetDetails part 3?

 

 

 

previous homenext

.

 

* If we depending on default natural sorting order internally JVM will Call CompareTo() method will inserting objects to the TreeSet. Hence the objects should be Comparable.
TreeSet t=new TreeSet();
t.add(“B”);
t.add(“z”); //”z”.compareTo(“B”); +ve
t.add(“A”); //”A”.compareTo(“B”); -ve
System.out.println(t); //[A,B,Z]

Note:
1. if we are not satisfied with default natural sorting order or if the default natural sorting order is not already available then we can define our own customized sorting by using comparator.
2. Comparable ment for Default Natural Sorting order where as Comparator ment for customized sorting order.