Hashtable Vs HashMap

Hashtable Vs HashMap

An interesting topic in collection framework is HashTable vs HashMap. Interviewers would love to ask questions on Hashtable. The basic difference every java programmer knows is Hashtable is synchronized while it’s not same with HashMap. There are some other differences between Hashtable and HashMap along with some common functionality as well, which we will be discussing in here.

Differences between HashMap and Hashtable.

[table “” not found /]

Similarities between HashMap and Hashtable.

  1. Both HashMap and Hashtable use principle of hashing to store key-value pair.
  2. Both HashMap and Hashtable don’t maintain insertion order.
  3. Both classes implements Map interface.
  4. Time required to perform put and get operation in both classes are same.

Uses of HashMap and Hashtable.

HashMap : When we are creating single threaded applications it is recommended to use HashMap because performance of HashMap is better as compared to Hashtable. Also, HashMap requires less space than Hashtable.

Hashtable : In multithreaded application, we can use Hashtable as it is internally synchronized. But in some scenarios it is recommended to use ConcurrentHashMap as it has some advantages over Hashtable in multithreaded environment. We will discuss advantages of ConcurrentHashMap in different blog.

Leave a Reply

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