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 >
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.
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.
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.
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.
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.
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 >
java.util.HashMap and java.util.Hashtable
iterators are fail-fast
The iterators returned by the iterator() method of the HashMap and Hashtable are fail-fast >
all three iterators are fail-fast
HashMap and Hashtable both does not maintain insertion order in java.
HashMap and Hashtable both are implementation of the java.util.Map interface in java.
Complexity offered by methods of HashMap and Hashtable both of these is same in java.
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.
Important Similarity and Differences in java >