What are the differences between a HashMap and a Hashtable in Java?
Which is more efficient for non-threaded applications?
There is many good answer already posted. I'm adding few new points and summarizing it.
HashMap
and Hashtable
both are used to store data in key and value form. Both are using hashing technique to store unique keys.
But there are many differences between HashMap and Hashtable classes that are given below.
HashMap
HashMap
is non synchronized. It is not-thread safe and can't be shared between many threads without proper synchronization code. HashMap
allows one null key and multiple null values. HashMap
is a new class introduced in JDK 1.2. HashMap
is fast. HashMap
as synchronized by calling this codeMap m = Collections.synchronizedMap(HashMap);
HashMap
is traversed by Iterator. HashMap
is fail-fast. HashMap
inherits AbstractMap class. Hashtable
Hashtable
is synchronized. It is thread-safe and can be shared with many threads. Hashtable
doesn't allow any null key or value. Hashtable
is a legacy class. Hashtable
is slow. Hashtable
is internally synchronized and can't be unsynchronized. Hashtable
is traversed by Enumerator and Iterator. Hashtable
is not fail-fast. Hashtable
inherits Dictionary class.Further reading What is difference between HashMap and Hashtable in Java?