Difference Between HashMap and Hashtable

[1838 views]


HashMap and Hashtable both of them are used to store data in key and value form. Both of them are using hashing technique to store unique keys. But there are many differences between HashMap and Hashtable classes that are given below.


HashMapHashtable
HashMap is not synchronized. Hence, HashMap performs better for non-threaded applications Hashtable is synchronized. Hence, HashTable is slower than HashMap for non-threaded applications
HashMap allows 1 null key and any number of 'null' values. Hashtable doesn't allow null key or values.
HashMap is a new class introduced in JDK 1.2. Hashtable is a legacy class.
HashMap is traversed by Iterator. Hashtable is traversed by Enumerator and Iterator.
We can make the HashMap as synchronized by calling this code Map m = Collections.synchronizedMap(HashMap); Hashtable is internally synchronized and can't be unsynchronized.
HashMap inherits AbstractMap class. Hashtable inherits Dictionary class.
Iterator in the HashMap is fail-fast eEnumerator for the Hashtable is not fail-fast and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator's own remove() method.
        

Want to Clear Java Interview? Try our Java Interview Notes



Comments



Search
Recommended Deals End in










Recommended Quiz
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    HashMap vs Hashtable