AdSense

AdSense3

Tuesday, 1 September 2015

Java HashSet class

  • uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
  • contains unique elements only.

Difference between List and Set:

List can contain duplicate elements whereas Set contains unique elements only.

Hierarchy of HashSet class:

HashSet class hierarchy

Example of HashSet class:

  1. import java.util.*;  
  2. class TestCollection9{  
  3.  public static void main(String args[]){  
  4.    
  5.   HashSet<String> al=new HashSet<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
       Vijay
       Ravi

No comments:

Post a Comment