Apr 13, 2012

HashSet Class

HashSet
·         Creates a collection that makes use of a hash table for storage.
·         A hash table is a data structure that stores information by mapping the key of each data element into an array position or index.
·         HashSet extends AbstractSet and implements the Set interface.
Example:
import java.util.HashSet;
import java.util.Set;
public class HashSetDemo
{
   public static void main(String[] args)
   {
      Set objSet = new HashSet();
      objSet.add("Harry");
      objSet.add("Emaini");
      objSet.add("Voldmort");
      objSet.add("Snape");
      objSet.add("Dumbuldore");
      System.out.println("Contents of the set :");
      System.out.println(objSet);
      System.out.println("Size of the set : " + objSet.size());
      System.out.println("\nContents of the set after adding 2 elements :");
      objSet.add("Tom Riddle");
      objSet.add("slytherin");
      System.out.println(objSet);
      System.out.println("Size of the set : " + objSet.size());
   }
}

0 comments :

Post a Comment