The HashMap and Hashtable, both are used to represent a group of objects that are represented in <Key, Value> pair. Each <Key, Value> pair is called Entry object. The collection of Entries is referred by the object of HashMap and Hashtable. Keys in a collection must be unique or distinctive.
The difference between HashMap and Hashtable is that HashMap particularly implements the Map interface whereas, the Hashtable extends the Dictionary class (legacy class) which is reengineered to implement Map interface. The other important difference is that objects of HashMap are unsynchronized whereas, the objects of Hashtable is synchronized.
Let’s view to comparison chart shown below to learn some more differences between HashMap and Hashtable.
Content: HashMap Vs Hashtable
Comparison Chart
Basis for Comparison | HashMap | Hashtable |
---|---|---|
Implement/ Extend | The HashMap class implements Map interface and extends an AbstractMap class. | The Hashtable extends of Dictionary Legacy class but, it is re-engineered and now it also implements Map interface. |
Synchronization | HashMap is unsynchronized, and hence, HashMap object is not threaded safe. | Hashtable is synchronized, and hence, the object of Hashtable is thread safe. |
Keys/Value | A key can return the Null only once, but a value can return Null any number of time. | A key can not return Null as it is used to obtain the hash code which will be used as an index of the hash table, nor a value can return Null. |
Default Initial Capacity | The default initial capacity of HashMap is 16. | The default initial capacity of Hashtable is 11. |
Traversing | HashMap is traversed by Iterator. | Like Map class Hashtable also does not directly support Iterator for traversing and hence, it uses Enumerator. |
Definition of HashMap
HashMap is a class that implements the Map interface and extends the AbstractMap class uses the hash table. The HashMap’s object refers to a collection/set of <Key, Value> pair where each key is mapped to a particular value. Keys in a collection must be unique as they are used to retrieve the value. On the other hand, the values in a collection can be duplicated.
The declaration of HashMap class and constructors of the HashMapclass are as follow:
/* K represents key, and V represents value */ class HashMap<K, V> /* Constructors of HashMap class*/ HashMap( ) HashMap(Map<? extends K, ? extends V> m) HashMap(int capacity) HashMap(int capacity, float fillRatio)
The first constructor is a default constructor that initializes an empty object of HashMap with a default capacity of 16 and default fill ratio of 0.75 . The second constructor initializes the hash map with the value of m. The third constructor creates a hash map with the initial capacity corresponding to the value provided in argument “capacity”. The fourth constructor initializes hash map with a capacity and a fill ratio provided in the parameters.
let us now learn how to feed the entries in a hash map.
Hashmap hm = new Hashmap(); hm.put("Ajay", 275); hm.put("Vijay", 250); hm.put("Jonny", 150); hm.put("Jordan", 200); System.out.println( hm); /*output*/ {Vijay=250, Jonny=150, Ajay=275, Jordan=200}
In above code, you can see that I created an empty HashMap object hm with default initial capacity and default fill ratio. Then I inserted four entries in the hash map using put(K,V) method that maps the key to the value. You can observe that entries are not printed in a sequence you feed them because the insertion order is not fixed.
Now, consider a case that you already have an entry<K1,V1> in the hash map and after that you try to insert put(K1, V5), i.e you try to map the same key with a different value. Then the put method will replace the old value V1 with the new value V2 and returns the old valueV1, otherwise, if we never try to replace a key’s value then put method returns Null for that key.
Definition of Hashtable
Hashtable is a class that extends the Dictionary class which is a legacy class and is reengineered to implement the Map interface. The Hashtable uses the hash table as its data structure. The Hashtable is similar to HashMap as here also the object of Hashtable refers to the collection of entries where each entry is a pair of <Key and Value>.
All the keys in a collection must be unique on the other hand, the values can be duplicated. The keys are particularly used to obtain the hash code value which decides the index, where the <Key, Value> pair will be stored in a hash table. In a hash table, neither a key nor a value can return Null pointer.
Let us see the declaration of Hashtable class and constructors of hashtable class.
/* K specifies the key and V specifies the value associated with the key*/ class Hashtable<K, V> /* constructors of Hashtable */ Hashtable( ) Hashtable(int size) Hashtable(int size, float fillRatio) Hashtable(Map<? extends K, ? extends V> m)
In above code, the first constructor is a default constructor which creates an empty object of a class Hashtable, its default size is 11 and default fill ratio is 0.75. The second constructor creates a hash table with the size corresponding to the value provided in the parameter “size”. The third constructor creates a hash table with a size and a fill ratio provided in the parameter. The fourth constructor initializes the hash table with the value m.
Let us now learn how to insert the <Key, Value> pair in the hash table.
Hashtable ht = new Hashtable(); ht.put(new hashCode(2), 275); ht.put(new hashCode(12), 250); ht.put(new hashCode(16), 150); ht.put(new hashCode(8), 200); System.out.println( ht); /*output*/ {12=250, 16=150,2y=275, 8=200}
In above code, I created an empty object of Hashtable and inserted four entries using put() method. Inside put method I called hashCode( ) which computes and return the hash code value which will act as the index value for entry object. As you can see I didn’t mention the size of hash table so by default it will be 11. Here also, insertion order is not preserved and hence, when printed entries did not appear in sequence it was fed.
Key Differences Between HashMap and Hashtable in Java
- The HashMap implements Map interface and extends an abstract class, AbstractMap whereas, the Hashtable extends the abstract class Dictionary, which is also a Legacy class, later reengineered to implement Map interface.
- The HashMap object is unsynchronized i.e. multiple threads can operate on it at the same time and hence, it objects not thread safe. On the other hand, the objects of Hashtable are synchronized i.e. if a thread wants to operate on an object of Hashtable it has to acquire a lock on that object so that no other thread could access that object and hence, it is thread safe.
- In HashMap key can return Null only once, and value can return Null multiple times. On the other hand, A key can never return Null as it used to obtain the hash code value which is used as an index to store the <Key, value> pair and nor a value can return Null.
- The default capacity of a hash table in the HashMap class is 16 whereas, the default capacity of a hash table in Hashtable is 11.
- The Iterator can traverse Hashmap entries. On the other hand, the Hashtable does not directly support the Iterator and hence, generally Enumerator is used for traversing the entries in Hashtable.
Similarities
- HashMap and Hashtable both uses data structure of the hash table.
- HashMap and Hashtable both implements Map interface
- Insertion order is not preserved in both HashMap and Hashtable and based on the hash code obtained using keys.
- In HashMap and Hashtable, Keys must be unique whereas, the values can be duplicated.
- HashMap and Hashtable both can contain heterogeneous objects for both keys and values.
- HashMap and Hashtable, both implements Serializable and Cloneable interfaces but not, random access.
- HashMap and Hashtable both have default fill ratio 0.75.
- HashMap and Hashtable both are best for retrieval or searching operations.
Conclusion
HashMap is better in performance as its objects are unsynchronized and multiple threads can operate on it at the same time and hence, it is faster than Hashtable.
kavya says
Nice article.