How is Hashtable different from HashMap?



In the last post we saw how HashMap works. Hashtable is another Map implementation which is commonly used. In this post let’s briefly touch upon how it works and see some major differences the two implementations have.

Working of Hashtable

The working of Hashtable is quite similar to HashMap. It also follows the same process

  • Key’s hashcode is used with mathematical operator like mod to get a bucket
  • An instance of Map.Entry is created and placed in the bucket

Let’s see it in pictures

hashtable-put

Hopefully, you might have spotted the difference. All the put/get operations are done under a lock. Hashtable used synchronized to acquire locks. This is a major difference between HashMap and Hashtable. There are other minor differences like Hashtable doesn’t support null values.

We encourage you to find more differences between the two and post in comments.

Leave a Reply

Your email address will not be published. Required fields are marked *