Friday, August 19, 2022

5 Difference between Hashtable vs HashMap in Java? Answer

Hashtable vs HashMap in Java
Hashtable and HashMap are two hash-based collections in Java and are used to store objects as key-value pairs. Despite being hash-based and similar in functionality there is a significant difference between Hashtable and HashMap and without understanding those differences if you use Hashtable in place of HashMap then you may run into series of subtle programs which is hard to find and debug. Unlike the Difference between ArrayList and HashMap, Differences between Hashtable and HashMap are more subtle because both are similar kinds of collections. Before seeing the difference between HashMap and Hashtable let's see some common things between HashMap and Hashtable in Java.



Similarities between Hashtable and HashMap in Java

There are a lot of similar things between Hashtable and HashMap in Java which is good to know and these also helps to find exactly what is different between HashMap and Hashtable in Java:

1. Common Parent
Both Hashtable and HashMap implements java.util.Map interface.

2. Common Underlying data structure
Hashtable and HashMap both are hash-based collections and works on the principle of hashing.

3. Common SLA
 Hashtable and HashMap both provide constant-time performance for the put and get method if objects are distributed uniformly across buckets.


4. Common Framework
From JDK 4 both Hashtable and HashMap are part of the Java collection framework.




Difference between Hashtable and HashMap in Java

Despite being so similar there are some differences between Hashtable and HashMap in Java which separates them completely, let's have a look :

1. Thread safety

The first and most significant difference between Hashtable and HashMap is that HashMap is not thread-safe while Hashtable is a thread-safe collection.

2. Performance

The second important difference between Hashtable and HashMap is performance since HashMap is not synchronized it perform better than Hashtable.

3. Old vs New

The third difference on Hashtable vs HashMap is that Hashtable is an obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.

4. Synchronized

Hashtable is a synchronized collection but HashMap is not. All methods of Hashtable are synchronized to prevent multithreading issues. 

5. Null Key

Since Hashtable is a synchronized collection it doesn't allow Null keys but HashMap does allow null keys and values. 

6. Fail fast (Iteraotr vs Enumeration)

Enumeration is used to iterate over keys and values in Hashtable which is not fail-fast, while Iterator is used to go over keys and values in HashMap and its fail-fast. 

Here is all the difference between HashMap and Hashtable in the nice tabular format: 






That's all about HashMap vs Hashtable in Java. These were some important differences between Hashtable and HashMap in Java. If you know any other difference which is not included here then feels free to add them in the comment section. Remember this is an important question on Java interview and good to prepare it well.

28 comments:

  1. HashMap permits null values in it, while Hashtable doesn't

    ReplyDelete
    Replies
    1. plus HashMap is slower than Hashtable because of the synchronoization factor.

      Delete
    2. Hi Anonymous HashMap is faster than HashTable -- HashMap is unsynchronized while hash table is synchronized

      Delete
  2. Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't

    ReplyDelete
  3. You can use LinkedHashMap with HashMap so you can have predictable iteration which is inseration order by default.

    ReplyDelete
  4. one common diff also is that hashtable not allowed any null keys or any null value where as hashmap can have oe null key and any more null values..

    ReplyDelete
  5. Nice ,
    But if given the thread safe example then it would be better to understand.

    ReplyDelete
  6. Hash table maintains order over a constant period of time whereas hash map doesnot mantian order

    ReplyDelete
  7. why hashtable not allowed any null keys or any null value where as hashmap can have oe null key and any more null values.

    ReplyDelete
  8. HashMap permits one null key and multiple null values.
    Hashtable doesn't permit any sort of nulls (key or values).

    ReplyDelete
  9. hashtable not allowed any null keys or any null value where as hashmap can have oe null key and any more null values..

    ReplyDelete
  10. Initial capacity of hashtable is 11 while it is 16 in case of hashmap..

    ReplyDelete
  11. HashMap class extends AbstractMap, whereas Hashtable extends the obsolete class Dictionary.

    and the popular class Properties extends Hashtable class neither HashMap nor ConcurrentHashMap

    ReplyDelete
    Replies
    1. Yes, that's true. Hashtable did extends Dictionary class but it also implement Map interface.

      Delete
  12. HashMap allows a null key multiple null values whereas hashtable neither allows null key nor values.

    ReplyDelete
    Replies
    1. @Anonmous, absolutely correct, btw, do you know why Hashtable doesn't allow null keys?

      Delete
  13. Hashtable is a legacy class and present from JDK 1, HashMap was added later

    ReplyDelete
  14. HashMap allows one null key and multiple null values where as
    Hashtable not allows any null keys and null values.

    ReplyDelete
    Replies
    1. @Nargendara, good point. Interviewer will then ask you how does HashMap resolve value for null key? remember, you cannot call equals() or hashcode() on null :-)

      Delete
    2. entry with null key always stored at index = 0

      Delete
  15. HashTable is legacy class whereas HashMap was adapted later in Java

    ReplyDelete
    Replies
    1. True, Hashtable is present from JDK 1.0 but HashMap was later added on Java 1.4

      Delete
  16. If we take HashMap it is not thread safe but want Thread safe means we need to go SynchronizedHashMap

    ReplyDelete
  17. HashMap allow one null key and multiple null values where as hashtable does't allow null values.

    ReplyDelete
    Replies
    1. I don't think Hashtable allows null keys as well. May need to double check.

      Delete

Feel free to comment, ask questions if you have any doubt.