All for Joomla All for Webmasters

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.

 

Example of HashSet class:

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

Leave a Reply

Top