What are the differences between HashMap and Hashtable?

+1 vote
asked Dec 15, 2016 by Hitesh Garg (799 points)  

I have to use hash collection in my project but I am not sure of which to use and exactly why? I am basically looking for some differences between a HashMap and a Hashtable in Java?

Which is more efficient for threaded and for non-threaded applications?

1 Answer

+1 vote
answered Apr 19, 2017 by clintwelbar (35 points)  
selected Apr 23, 2017 by Hitesh Garg
Best answer
  • Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
  • In hashmap you can remove element while iterating, safely. where as
    it is not possible in hashtables.
  • One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order , you could easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
  • Considering performance, Hashmap is much faster and uses less memory
    than Hashtable
    as former is unsynchronized . Unsynchronized objects
    are often much better in performance in compare to synchronized
    object like Hashtable in single threaded environment.

More on..... Hashmap