Hashtable vs HashMap in Java
Hashtable and HashMap are two hash based collection in Java and used to store objects as key value pair. Despite being hash based and similar in functionality there are a significant difference between Hashtable and HashMap and without understanding those difference if you use Hashtable in place of HashMap than you may run into series of subtle programs which is hard to find and debug.Unlike Difference between ArrayList and HashMap, Difference between Hashtable and HashMap are more subtle because both are similar kind of collection. Before seeing 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 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) Both Hashtable and HashMap implements java.util.Map interface.
2) Hashtable and HashMap both are a hash based collection and works on the principle of hashing.
3) Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket.
4) From JDK 4 both Hashtable and HashMap are part of Java collection framework.
Despite being so similar there are some differences between Hashtable and HashMap in Java which separates them completely, let's have a look :
1) First and most significantly different between Hashtable and HashMap are that HashMap is not thread-safe while Hashtable is a thread-safe collection.
2) The second important difference between Hashtable and HashMap is performance since HashMap is not synchronized it perform better than Hashtable.
3) The third difference on Hashtable vs HashMap is that Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.
These were some important difference on 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.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java
Difference between TreeSet and HashSet in Java
Difference between HashMap and ArrayList in Java
Hashtable and HashMap are two hash based collection in Java and used to store objects as key value pair. Despite being hash based and similar in functionality there are a significant difference between Hashtable and HashMap and without understanding those difference if you use Hashtable in place of HashMap than you may run into series of subtle programs which is hard to find and debug.Unlike Difference between ArrayList and HashMap, Difference between Hashtable and HashMap are more subtle because both are similar kind of collection. Before seeing 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 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) Both Hashtable and HashMap implements java.util.Map interface.
2) Hashtable and HashMap both are a hash based collection and works on the principle of hashing.
3) Hashtable and HashMap both provide constant time performance for put and get method if objects are distributed uniformly across bucket.
4) From JDK 4 both Hashtable and HashMap are part of Java collection framework.
Difference between Hashtable and HashMap in Java

1) First and most significantly different between Hashtable and HashMap are that HashMap is not thread-safe while Hashtable is a thread-safe collection.
2) The second important difference between Hashtable and HashMap is performance since HashMap is not synchronized it perform better than Hashtable.
3) The third difference on Hashtable vs HashMap is that Hashtable is obsolete class and you should be using ConcurrentHashMap in place of Hashtable in Java.
These were some important difference on 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.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java
Difference between TreeSet and HashSet in Java
Difference between HashMap and ArrayList in Java
HashMap permits null values in it, while Hashtable doesn't
ReplyDeleteplus HashMap is slower than Hashtable because of the synchronoization factor.
DeleteHi Anonymous HashMap is faster than HashTable -- HashMap is unsynchronized while hash table is synchronized
DeleteYes.
DeleteAnother difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't
ReplyDeleteYou can use LinkedHashMap with HashMap so you can have predictable iteration which is inseration order by default.
ReplyDeleteone 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..
ReplyDeleteNice ,
ReplyDeleteBut if given the thread safe example then it would be better to understand.
Hash table maintains order over a constant period of time whereas hash map doesnot mantian order
ReplyDeletewhy hashtable not allowed any null keys or any null value where as hashmap can have oe null key and any more null values.
ReplyDeleteHashMap permits one null key and multiple null values.
ReplyDeleteHashtable doesn't permit any sort of nulls (key or values).
cooll...
Deletehashtable not allowed any null keys or any null value where as hashmap can have oe null key and any more null values..
ReplyDeleteInitial capacity of hashtable is 11 while it is 16 in case of hashmap..
ReplyDeleteHashMap class extends AbstractMap, whereas Hashtable extends the obsolete class Dictionary.
ReplyDeleteand the popular class Properties extends Hashtable class neither HashMap nor ConcurrentHashMap
Yes, that's true. Hashtable did extends Dictionary class but it also implement Map interface.
DeleteHashMap allows a null key multiple null values whereas hashtable neither allows null key nor values.
ReplyDelete@Anonmous, absolutely correct, btw, do you know why Hashtable doesn't allow null keys?
DeleteHashtable is a legacy class and present from JDK 1, HashMap was added later
ReplyDeleteHashMap allows one null key and multiple null values where as
ReplyDeleteHashtable not allows any null keys and null values.
@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 :-)
Deleteentry with null key always stored at index = 0
DeleteHashTable is legacy class whereas HashMap was adapted later in Java
ReplyDeleteTrue, Hashtable is present from JDK 1.0 but HashMap was later added on Java 1.4
DeleteIf we take HashMap it is not thread safe but want Thread safe means we need to go SynchronizedHashMap
ReplyDeleteHashMap allow one null key and multiple null values where as hashtable does't allow null values.
ReplyDelete