Friday, 11 July 2014

Differences between Hashtable and HashMap

Before we actually see differences,let me give you brief introduction of both.

HashMap implements Map interface which maps key to value.It is not synchronized and is not thread safe.Duplicate keys are not allowed and null keys as well as value are allowed. 
HashMap employeeHashmap=new HashMap();  
employeeHashmap.put(2,null);  // will work fine 
Hashtable Hashtable implements Map interface which maps key to value.It is synchronized and thread safe.Duplicate keys are not allowed and null key is not allowed.
Hashtable employeeHashmap=new Hashtable();  
employeeHashmap.put(2,null);  //not allowed and will throw NullPointer exception at run time

Hashtable vs HashMap:

Due to theadSafe and Synchronized,it is often slower than HashMap
In single threaded environment, it is much faster than Hashtable.So if you do not work in multi thread environment ,then hashMap is recommended
Null key
Do not allow
Allows null key as well as values
Fail fast
enumeration in hashtable is not fail fast
Iterator in hashMap is fail fast
It extends Dictionary class which is quite old
It extends AbstractMap class
No alternative
You can use ConcurrentHashMap for multi thread environment

Some important points need to be discussed. 
  • Synchonized meaning only one thread can modify one table  at one point of time.When any thread perform update operation on hashtable then it acquires lock on it and other threads have to wait for lock to be released.
  • Fail-fast iterator means if one thread is iterating over hashmap and other thread trying to modify hashmap structurally it will throw ConcurrentModification Exception and fail immediately.Structurally modification means inserting or deleting elements that can change structure of map.

Can we synchronize HashMap?

Yes,We can synchonized a HashMap also with the help of Collections.synchonizedMap(hashmap) so HashMap can be synchronized by

Map map=Collections.synchonizedMap(hashmap)  


  1. This comment has been removed by the author.

  2. Thanks for sharing the very useful info about Spring and please keep updating........

  3. I have seen lot blogs and Information on othersites But in this Java Blog Information is very useful thanks for sharing it........

  4. Thank you for Differentiating the Hash-table and Hash-map As these will be useful for some projects which may be having some set of modules As I being a PMP Certified and Completed my PMP in Chennai, I was handling some major projects which were having huge module structure where they are being a time taking process Anywayz Thank you for explaining the difference of these Please KeepUpdating

  5. Hi, thank you for providing the information regarding Hash-table and Hash-map Well When I was having my PMP Training in Mumbai I was having a bit confusion of these Hash-table and Hash-map After seeing your content its clear and good Once again Thank you for providing the information Keep updating

  6. Thanks for sharing this valuable information to our vision Full Stack Training in Hyderabad

  7. Each department of CAD have specific programmes which, while completed could provide you with a recognisable qualification that could assist you get a job in anything design enterprise which you would really like.

    AutoCAD training in Noida

    AutoCAD training institute in Noida

    Best AutoCAD training institute in Noida


Related Posts Plugin for WordPress, Blogger...