AdSense

AdSense3

Thursday 3 September 2015

Java TreeSet class

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

Hierarchy of TreeSet class:

TreeSet class hierarchy

Example of TreeSet class:

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

No comments:

Post a Comment