In this Collection framework tutorial we will learn what are differences and similarities 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.
| |
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
|
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.
|
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 >