Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 7, 2021 in Java by dante07 (13.1k points)

Browse Categories

...