Home » Core Java » Difference between HashMap and HashTable

Difference between HashMap and HashTable

What is the difference between hashmap and hastable ?
This interview question is getting popular now-a-days and almost every company asks this question.The answer of this question shows your collection framework knowledge,your multi threading knowledge and your Exception handling knowledge.Let me put the answer in the tabular format so that it will easier for you to understand it.


HashMap HashTable
HashMap Extends Abstract Map class. HasTable extends Dictonary Class.
HashMap allows maximum one null key. HashTable does not allow null key.
HashMap allows any number of  null values. HashTable does not  allows  null values.
HashMap is not synchronized ,so it is not thread safe. HashTable is synchronized ,so it is  thread safe.
HashMap is fast as there is no syncronization involved. HashTable is slow because of  synchronization involved.
HashMap is preferred in Single Thread application. HashTable is preferred in Multi Threaded application.
Iterators returned by HashMap are fail-fast  in nature. Iterators returned by HashTable are fail-safe in nature.

To add more clarity to this comparison table ,let me explain to you what is fail safe and fail fast.This is another important follow up question interviewer may ask you “What is the difference between fail-fast and fail-safe iterators ?

Fail Fast Iterators fail as soon as the Collection structure is changed during traversal.Suppose first thread is iterating over a collection and at the same another thread has added/removed/ modified any element of collection ,so for first thread during its next iteration,it does not find elements of the collection in consistent state and it throws ConcurrentModificationException.

Fail Safe iterator is exactly opposite of fail fast and it does not throw any Exception even if the structure of collection is changed.In multi-threaded environment fail safe iterators work on clone of the collection’s object.So,it does not throw ConcurrentModificationException exception.Iterator of CopyOnWriteArrayList and ConcurrentHashMap are fail safe.

Leave a comment

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

18 − seven =