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. This comment has been removed by the author.

  8. A career in vehicle CAD (computer aided design) means you get to apply the cutting-edge computer era to help layout and draft automotive components. In this visual and symbol-based verbal exchange approach, you will be capable of tailor your understanding to the automotive area and to the profession within the car area which you want to go into into. This will ensure a protracted-term, high tech profession route that is now not only applicable today, but for the next day as nicely. The following kinds of individuals may be excellent candidates for a career in auto CAD:

    AutoCAD training in Noida

    AutoCAD training institute in Noida

    Best AutoCAD training institute in Noida

    Company address:
    Webtrackker Technology
    C- 67, Sector- 63, Noida
    Phone: 01204330760, 8802820025

  9. Cloud Computing Training In Noida
    Webtrackker is IT based company in many countries. Webtrackker will provide you a real time projects based training on Cloud Computing. If you are looking for the Cloud computing training in Noida then you can join the webtrackker technology.
    Cloud Computing Training In Noida , Cloud Computing Training center In Noida , Cloud Computing Training institute In Noida ,

    Company Address:
    Webtrackker Technology
    C- 67, Sector- 63, Noida

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

    MSBI Training in Hyderabad

  11. 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
    Best kitchen chimney installation and service
    Auto clean chimney sevice in Faridabad

  12. Merry Christmas,
    Very nice and informative post. Please keep it up. If you want to read Christmas Message For Parents you have to see the link
    Thank you,

  13. Hi,
    The very next time I read a blog, I hope that it does not disappoint me as much as this particular one.New Year Greetings I mean, I know it was my choice to read, but I truly thought you would probably have something useful to say. All I hear is a bunch of crying about something that you could possibly fix if you were not too busy searching for attention.

  14. I accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.we are providing ReactJs training in Chennai.
    For more details: ReactJs training in Velachery | ReactJs training in chennai


Related Posts Plugin for WordPress, Blogger...