Apr 13, 2012

HashMap Class

public class HashMap extends AbstractMap implements Map
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Constructor
HashMap() Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).
HashMap(int initialCapacity) Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
HashMap(int initialCapacity, float loadFactor) Constructs an empty HashMap with the specified
initial capacity and load factor.
HashMap(Map<? extends K,? extends V> m) Constructs a new HashMap with the same mappings
as the specified Map.


Example:
import java.util.*;
class Hashtest
{
   public static void main(String args[])
   {
      Hashtable ht = new Hashtable();
      Enumeration enume;
      String strName;
      ht.put("Tony",new String("2001"));
      ht.put("Cathy",new String("2002"));
      ht.put("Michael",new String("2002"));
      ht.put("Priscilla",new String("2001"));
      ht.put("Mark",new String("2001"));
      enume = ht.keys();
      while(enume.hasMoreElements())
      {
         strName = (String)enume.nextElement();
         System.out.println(strName + " completed graduation in " +
         ht.get(strName) + "\n");
      }
   }
} 

0 comments :

Post a Comment