Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (3.5k points)
What are the differences between a HashMap and a Hashtable in Java?

Which is more efficient for non-threaded applications?

1 Answer

0 votes
by (46k points)

There are many differences between HashMap and Hashtable in Java:

  • Hashtable remains synchronized, whereas HashMap is not. This makes HashMap favorably for non-threaded applications, as unsynchronized Objects typically make more useful than synchronized ones.
  • Hashtable doesn't support null keys or values. HashMap provides one null key and any number of null values.
  • One of HashMap's subclasses is LinkedHashMap, so if you'd want a predictable repetition order (which is addition order by default), you could simply swap out the HashMap for a LinkedHashMap. This wouldn't be as simple if you were applying Hashtable.

Following synchronization isn't a concern for you, I'd suggest  HashMap. If synchronization displays an issue, you may additionally look at ConcurrentHashMap.

Browse Categories