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.
- Both HashMap and Hashtable use principle of hashing to store key-value pair.
- Both HashMap and Hashtable don’t maintain insertion order.
- Both classes implements Map interface.
- 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.