Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

What is difference between HashMap and Hashtable?

Category: Java

HashMap and Hashtable both implements Map interface and looks similar, however there are following difference between HashMap and Hashtable.

  • HashMap allows null key and values whereas Hashtable doesn’t allow null key andvalues.
  • Hashtable is synchronized but HashMap is not synchronized. So HashMap is better for single threaded environment, Hashtable is suitable for multi-threaded environment.
  • LinkedHashMap was introduced in Java 1.4 as a subclass of HashMap, so incase you want iteration order, you can easily switch from HashMap to LinkedHashMap but that is not the case with Hashtable whose iteration order is unpredictable.
  • HashMap provides Set of keys to iterate and hence it’s fail-fast but Hashtable provides Enumeration of keys that doesn’t support this feature.
  • Hashtable is considered to be legacy class and if you are looking for modifications of Map while iterating, you should use ConcurrentHashMap.

Leave a Reply

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