What are the differences between a HashMap and a Hashtable in Java?
Which is more efficient for non-threaded applications?
What are the differences between a HashMap and a Hashtable in Java?
Which is more efficient for non-threaded applications?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Sep 21, 2022, 5:41 AM EDT
Hi Jaya,
There are several differences between HashMap and Hashtable in Java:
Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
One of HashMap's subclasses is LinkedHashMap, so if you'd want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
Since synchronization is not an issue for you, I'd recommend HashMap. If synchronization becomes an issue, you may also look at ConcurrentHashMap
Amit KumarRank #459 · 5.4K reputationPosted Sep 21, 2022, 6:37 AM EDT
Hi ,
Differences are.
For more detail
https://www.geeksforgeeks.org/differences-between-hashmap-and-hashtable-in-java/#:~:text=Hashmap%20vs%20Hashtable&text=It%20is%20thread%2Dsafe%20and,thread%20synchronization%20is%20not%20needed.
Dhanush KRank #1,037 · 1.9K reputationPosted Oct 21, 2024, 8:29 AM EDT
Hashtableis synchronized. If a thread-safe implementation is not needed, it is recommended to useHashMapin place ofHashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to useConcurrentHashMapin place ofHashtable.