Difference Between HashMap and Hashtable
There exist differences between HashMap and Hashtable such as Hashtable is synchronized and HashMap is not synchronized. Other differences between the two data structures are shown in the table provided below:
Key Differences between HashMap and Hashtable
HashMap | Hashtable |
It is non-synchronized. | It is synchronized. |
Thread-safe. | Thread-unsafe. |
Faster than Hashtable. | Slower than HashMap. |
It can allow one null key. | Can not allow null key. |
Traversed by Iterator. | Traversed by Enumerator and Iterator. |
Carries fail-fast iterator. | Does not carry a fail-fast iterator. |
AbstractMap class is inherited. | Dictionary class is inherited. |
Can carry various null values. | Can not carry null values. |
What is HashMap in Java?
The HashMap is used for the implementation of the Map interface in Java. Since it is a type of data structure, it is used to save the data. The data is stored in the form of keys and values. The key and value can be accessed with the index of another type. The HashMap is unsynchronized.
The single null key and multiple null values can be stored with the HashMap. The syntax of the HashMap is shown below:
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
Where, K is the type of the keys on the map, and V is the type of values mapped on the map.
What is Hashtable in Java?
Hashtable is also a type of data structure and hence is used to store or arrange data. This data structure also stores data in the form of key and value pairs. The hashtable comprises the following parts:
- Hash Function
- Array
In Hashtable, non-null objects can be stored as a value and key pairs but null values are not stored in this, unlike HashMap. This is a synchronized way of storing data. The syntax of the Hashtable is shown below:
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
Where, K is the type of the keys on the map, and V is the type of values mapped on the map.
Check out some important topics related to the difference between HashMap and Hashtable in the table provided below:
Comments
write a comment