Differences and Similarities between HashMap and Hashtable in java


In this Collection framework tutorial we will learn what are differences and similarities between java.util.HashMap and java.util.Hashtable in java.




Differences between java.util.HashMap and java.util.Hashtable in java >



Property
java.util.HashMap
java.util.Hashtable
1
synchronization
java.util.HashMap is not synchronized  (because 2 threads on same HashMap object can access it at same time) in java.

java.util.Hashtable is synchronized (because 2 threads on same Hashtable object cannot access it at same time) in java.
2
Performance
HashMap is not synchronized, hence its operations are faster as compared to Hashtable in java.
Hashtable is synchronized, hence its operations are slower as compared to HashMap in java.

If we are working not working in multithreading environment jdk recommends us to use HashMap.
3
Null keys and values
HashMap allows to store one null key and many null values i.e. many keys can have null value in java.
Hashtable does not allow to store null key or null value.
Any attempt to store null key or value throws runtimeException (NullPointerException) in java.
4
Introduced  in which java version
HashMap was introduced in second version of java i.e. JDK 2.0
Hashtable was introduced in first version of java i.e. JDK 1.0
But it was refactored in java 2 i.e. JDK 1.2 to implement the Map interface, hence making it a member of member of the Java Collections Framework.
5
Recommendation
In non-multithreading environment it is recommended to use HashMap than using Hashtable in java.
In java 5 i.e. JDK 1.5, it is recommended to use ConcurrentHashMap than using Hashtable.
6
Extends Dictionary (Abstract class, which is obsolete)
HashMap does not extends Dictionary in java.
Hashtable extends Dictionary (which maps non-null keys to values. In a given Dictionary we can look up value corresponding to key) in java.


So far we have learned what are differences between java.util.HashMap and java.util.Hashtable in java. Now we will learn similarities between java.util.HashMap and java.util.Hashtable in Collection framework in java.


Similarity in java.util.HashMap and java.util.Hashtable >

Property
java.util.HashMap and java.util.Hashtable
1
iterators are fail-fast
The iterators returned by the iterator() method of the HashMap and Hashtable are fail-fast >
  1. map.keySet().iterator()
  2. map.values().iterator()
  3. map.entrySet().iterator()

all three iterators are fail-fast
2
Insertion order
HashMap and Hashtable  both does not maintain insertion order in java.
3
Implements java.util.Map
HashMap and Hashtable both are implementation of the java.util.Map interface in java.
4
Complexity
Complexity offered by methods of HashMap and Hashtable both of these is same in java.

Operation/ method
Worst case
Best case
put(K key, V value)
O(n)
O(1)
get(Object key)
O(n)
O(1)



So in this Collection framework tutorial we learned what are differences and similarities between java.util.HashMap and java.util.Hashtable in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Important Similarity and Differences in java >

Collection vs Collections - Differences in java

ArrayList vs LinkedList - Similarity and Differences in java


HashMap and ConcurrentHashMap - Similarity and Differences in java


HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences in java


HashMap vs IdentityHashMap - Similarity and Differences with program in java


TreeMap vs ConcurrentSkipListMap - Similarity and Differences with program in java


Map hierarchy tutorial in java - Detailed - java.util.HashMap, java.util.Hashtable, java.util.concurrent.ConcurrentHashMap, java.util.LinkedHashMap, java.util.TreeMap, java.util.concurrent.ConcurrentSkipListMap, java.util.IdentityHashMap, java.util.WeakHashMap, java.util.EnumMap classes


eEdit
Must read for you :