Both HashMap and Hashtable are used for storing key value pairs and use hash based data structures but have subtle differences from implementation perspective. In this article, we shall the differences between the two and the advise on when to use which collection class.

In fact, this question is also part of many Java interviews. This page will be your source for all differences that are present in the two classes. Apart for the difference between hashmap and hashtable, difference between ArrayList and Vector is also very frequently asked in Java interview.

Difference between HashMap and Hashtable

  • 1) The very common difference which every Java developer should know is that while Hashtable is synchronized, HashMap is not. By synchronized, I mean the methods of Hashtable class are marked as synchronized. Many other synchronized map classes had been introduced in JDK 5. It doesn’t make any sense to use Hashtable just for the sake thread safety after ConcurrentHashMap class was introduced in JDK 1.5.
  • 2) Hashtable extends Dictonary class where as HasMap extends the AbtractMap class.
  • 3) Hashtable doesn’t allow null keys or values. On the other hand HashMap allows the use of null as key and values. Since the key has to be unique with HashMap, only one null key can be used. If you try using null as key or value for Hashtable, java.lang.NullPointerExcpetion shall be raised.
  • 4) The sub classes of Hashtable include, Properties.java, CacheTable.java and MultiCacheTable.java. The sub classes of HashMap include LinkedHashMap and ProcessEnviornment. It is worth mentioning here that if you want to maintain sorted order in HashMap, use its sub class LinkedHashMap.
  • 5) With so many variants of Map related classes being available, the use of Hashtable is recommended only when some legacy API needs it. HashMap is used in applications with no thread safety requirements.
  • 6) For iterating the Hashtable, there is enumerator available by using the elements() method on hashtable. If you want to iterate the key value pair of HashMap, you need to get the keyset and then iterate the keyset using iterator and invoke get method on hashmap with argument as the key object.
  • 7) When iterating the Hashtable there is no ConcurrentModificationException raised whereas HashMap’s iterator provides fail fast behavior where in the iterator will throw ConcurrentModificationException if the HasMap is modified using any other method than the iterator.
  • 8) Hashtable has been present in JDK since 1.0 version where as HashMap was added in JDK 1.2.
  • 9) Hashtable and HashMap classes have same authors i.e. Arthur van Hoff, Josh Bloch and Neal Gafter with the only difference that Doug Lea also contributed to HashMap but didn’t play any role in Hashtable class.
  • 10) In terms of performance, HashMap scores better as its methods are not synchronized where as multiple threads can not invoke the hashtable methods on the same hashtable instance simultaneously.
Differences between HashMap and Hashtable in Java admin Core Java
Both HashMap and Hashtable are used for storing key value pairs and use hash based data structures but have subtle differences from implementation perspective. In this article, we shall the differences between the two and the advise on when to use which collection class. In fact, this question is also...
Both HashMap and Hashtable are used for storing key value pairs and use <a href="http://www.javaexperience.com/java-dive-deep-into-hashcode-internals/">hash based data structures</a> but have subtle differences from implementation perspective. In this article, we shall the differences between the two and the advise on when to use which collection class. In fact, this question is also part of many Java interviews. This page will be your source for all differences that are present in the two classes. Apart for the difference between hashmap and hashtable, difference between ArrayList and Vector is also very frequently asked in Java interview. <h2>Difference between HashMap and Hashtable</h2> <ul> <li>1) The very common difference which every Java developer should know is that while <strong>Hashtable is synchronized, HashMap is not</strong>. By synchronized, I mean the methods of Hashtable class are marked as synchronized. Many other synchronized map classes had been introduced in JDK 5. It doesn't make any sense to use Hashtable just for the sake thread safety after ConcurrentHashMap class was introduced in JDK 1.5.</li> <li>2) Hashtable extends Dictonary class where as HasMap extends the AbtractMap class.</li> <li>3) Hashtable doesn't allow null keys or values. On the other hand HashMap allows the use of null as key and values. Since the key has to be unique with HashMap, only one null key can be used. If you try using null as key or value for Hashtable, <a href="http://www.javaexperience.com/java-null-value-and-nullpointerexception/">java.lang.NullPointerExcpetion shall be raised</a>.</li> <li>4) The sub classes of Hashtable include, Properties.java, CacheTable.java and MultiCacheTable.java. The sub classes of HashMap include LinkedHashMap and ProcessEnviornment. It is worth mentioning here that if you want to maintain sorted order in HashMap, use its sub class LinkedHashMap.</li> <li>5) With so many variants of Map related classes being available, the use of Hashtable is recommended only when some legacy API needs it. HashMap is used in applications with no thread safety requirements. </li> <li>6) For iterating the Hashtable, there is enumerator available by using the elements() method on hashtable. If you want to iterate the key value pair of HashMap, you need to get the keyset and then iterate the keyset using iterator and invoke get method on hashmap with argument as the key object. </li> <li>7) When iterating the Hashtable there is no ConcurrentModificationException raised whereas HashMap's iterator provides fail fast behavior where in the iterator will throw ConcurrentModificationException if the HasMap is modified using any other method than the iterator. </li> <li>8) Hashtable has been present in JDK since 1.0 version where as HashMap was added in JDK 1.2.</li> <li>9) Hashtable and HashMap classes have same authors i.e. Arthur van Hoff, Josh Bloch and Neal Gafter with the only difference that Doug Lea also contributed to HashMap but didn't play any role in Hashtable class.</li> <li>10) In terms of performance, HashMap scores better as its methods are not synchronized where as multiple threads can not invoke the hashtable methods on the same hashtable instance simultaneously.</li> </ul>
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.