• The underlying data structure is Hashtable.
  • Hashing insertion order is not preserve, because it based on Hashcode of keys.
  • Duplicate keys are not allowed. But values can be duplicated.
  • Heterogeneous objects allowed for both keys& value.
  • Null key is allowed.
  • Null values are allowed, any null of times.

Difference between HashMap& Hashtable:-



  • No method is synchronized.
  • Performance is fast/ high
  • HashMap objects is not thread safe.
  • Null is allowed for both keys& values.
  • It is non legacy, introduced in 1.2 version.



  • Every method is the synchronized.
  • Hashtable object is thread safe.
  • Performance is low.
  • Null, we can’t use for keys & value violation leads to NullPointerException.
  • it is legacy& introduced in 1.0 version.

Collections class contain synchronized Map method:-

HashMap m = new HashMap ();

Map m2 = collections. synchronizedMap (m);

Constructors of HashMap:-

  1. HashMap h = new HashMap ();// creates an empty HashMap object, with default initial capacity& default fill ratio 0.7.
  2. HashMap h = new HashMap ( int initial capacity)
  3. HashMap h = new HashMap ( int initialcapacity, float fillratio);
  4. HashMap h = new HashMap ( Map m )
import java. util. *;
Class HashMapDemo
Public static void main ( String[] args)
            HashMap m = new HashMap();
            m. put("narayana", 700);
            m. put("anitha", 800);
            m.put("sunny", 200);
            m. put("vighnesh", 500);
            Set s = m.keySet();
            collection c = m.values();
            System.out.println(c);// 500, 800, 200, 1000
            Set s1 = m.entrySet();
            iterator itr = s1. iterator();
             while ( its.hasNext())
            Map.Entry m1 =( Map.Entry) itr.next();

The Author


Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply