All for Joomla All for Webmasters

Java TreeSet class

  • contains unique elements only like HashSet. The TreeSet class implements NavigableSet interface that extends the SortedSet interface.
  • maintains ascending order.

Example of TreeSet class:

  1. import java.util.*;
  2. class TestCollection11{
  3.  public static void main(String args[]){
  4.   TreeSet<String> al=new TreeSet<String>();
  5.   al.add(“Ravi”);
  6.   al.add(“Vijay”);
  7.   al.add(“Ravi”);
  8.   al.add(“Ajay”);
  9.   Iterator<String> itr=al.iterator();
  10.   while(itr.hasNext()){
  11.    System.out.println(itr.next());
  12.   }
  13.  }
  14. }
Output:Ajay
       Ravi
       Vijay
PinIt
submit to reddit

Leave a Reply

Top