Friday 11 July 2014

Differences between Hashtable and HashMap

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

HashMap
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(1,"Harit");  
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(1,"Harit");  
employeeHashmap.put(2,null);  //not allowed and will throw NullPointer exception at run time


Hashtable vs HashMap:

Parameter
Hashtable
HashMap
ThreadSafe
Yes
No
Synchronized
Yes
No
Performance
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
Extends
It extends Dictionary class which is quite old
It extends AbstractMap class
Alternative
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)  

14 comments:

  1. This comment has been removed by the author.

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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Your blog is very useful for me, Thanks for your sharing.


    MSBI Training in Hyderabad


    ReplyDelete
  8. Like different elements of India, numerous oil and spices usually cross into making food. This effects in substances getting caught to the partitions of the filter out.
    Visit here
    http://kitchenware.ml
    Best kitchen chimney installation and service
    Auto clean chimney sevice in Faridabad

    ReplyDelete
  9. Usually, I come thorough multiple blogs daily but today I found your post very relevant and informative. It is really appreciable work by you. Good luck with the upcoming work.

    Digital Marketing Services in delhi
    website development packages

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...