Last Updated: 22 January, 2023
HashMap and Hashtable both implement the Map interface and accept values in the form of key-value pairs. Both the classes look similar, but there are many differences between them.
The HashMap class was introduced in the Java 1.2 version and is available in the java.util package. HashMap is a child class of the AbstractMap class, and it implements the Map interface as well as the Cloneable and Serializable interfaces.
HashMap is used to store the data in the form of key-value pairs, where keys must be unique and values may be duplicated. HashMap accepts only one null key and multiple null values.
Java HashMap Class Declaration
Here, K is the key Object type and V is the value Object type.
Java HashMap important features:
Hashtable is a legacy class in Java. It was introduced in the JDK 1.0 version and is available in the java.util package.
Hashtable stores the data in the form of key-value pairs, where keys must be unique and values may be duplicates.
Hashtable does not allow null as a key or value. If we add NULL as a key or value, then at runtime it will throw a NullPointerException.
Hashtable is by default a synchronised class in Java.
Java Hashtable Class Declaration
Here, K is the key Object type and V is the value Object type.
Comparison Parameters | HashMap Vs Hashtable |
---|---|
Synchronized | HashMap is not synchronized by default, whereas HashTable is by default synchronized. |
Thread Safe | HashMap is not thread safe, whereas HashTable is thread safe. |
Null keys and null values | Hashmap allows one null key and any number of null values, whereas Hashtable does not allow null keys or null values. |
Iterating the values | Hashmap object values are iterated by using an iterator, whereas HashTable is the only class other than vector that uses an enumerator to iterate the values of HashTable objects. |
Fail-fast iterator | The iterator in Hashmap is a fail-fast iterator, while the enumerator for Hashtable is not. If the Hashtable is structurally modified at any time after the iterator is created in any way except the iterator's own remove method, then the iterator will throw a Note: Structural modification means adding or removing elements from the Collection object. Thus, the enumerations returned by the Hashtable keys and elements methods are not fail-fast. |
Performance | Hashmap is much faster and uses less memory than Hashtable as the former is unsynchronized. In a single-threaded environment, unsynchronized objects are often much better in performance when compared to synchronized objects like hashtables. |
Superclass | Hashtable is a subclass of the Dictionary class, which is now obsolete in JDK 1.7, so it is not used anymore. It is better off externally synchronizing a HashMap or using a ConcurrentMap implementation (e.g., ConcurrentHashMap). HashMap is a subclass of the AbstractMap class. Although Hashtable and HashMap have different superclasses, they are both implementations of the "Map" abstract data type. |
Legacy Class | HashMap is not a legacy class, whereas Hashtable is a legacy class in Java. |
Introduced | HashMap was introduced in the JDK 1.2 version, whereas Hashtable was introduced in the JDK 1.0 version. |
Output
Iterating Hashmap and printing Key and Value
in - India
en - England
us - United State
ca - Canada
Output
Iterating Hashtable and printing Key and Value
700 - Python
400 - C
200 - C++
100 - Java
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com