Differences between HashMap and Hashtable in java
In Java HashMap and HashTable are two important data structures in the Collection Framework which have some common things(features) between them. Both implement Map interface, store the data in the form of key-value pairs and use Hashing technique to store the elements.
But, there also exist significant differences between them. One important difference being the thread safety. HashMap is not thread safe where as HashTable is thread safe. In this post, we will discuss the differences and similarities between HashMap Vs HashTable in java.
Differences Between HashMap And HashTable In Java :
1) Thread Safe
HashTable is internally synchronized. Therefore, it is very much safe to use HashTable in multi threaded applications. Where as HashMap is not internally synchronized. Therefore, it is not safe to use HashMap in multi threaded applications without external synchronization. You can externally synchronize HashMap using Collections.synchronizedMap() method.
2) Inherited From
Though both HashMap and HashTable implement Map interface, but they extend two different classes. HashMap extends AbstractMap class where as HashTable extends Dictionary class which is the legacy class in java.
3) Null Keys And Null Values
HashMap allows maximum one null key and any number of null values. Where as HashTable doesn’t allow even a single null key and null value.
4) Traversal
HashMap returns only Iterators which are used to traverse over the elements of HashMap. HashTable returns Iterator as well as Enumeration which can be used to traverse over the elements of HashTable.
5) Fail-Fast Vs Fail-Safe
Iterator returned by HashMap are fail-fast in nature i.e they throw ConcurrentModificationException if the HashMap is modified after the creation of Iterator other than iterator’s own remove() method. On the other hand, Enumeration returned by the HashTable are fail-safe in nature i.e they don’t throw any exceptions if the HashTable is modified after the creation of Enumeration.
6) Performance
As HashTable is internally synchronized, this makes HashTable slightly slower than the HashMap.
7) Legacy Class
HashTable is a legacy class. It is almost considered as due for deprecation. Since JDK 1.5, ConcurrentHashMap is considered as better option than the HashTable.
8) Member Of Java Collection Framework
HashMap is a member of Java Collection Framework right from the beginning of its introduction in JDK 1.2. But, HashTable was there before JDK 1.2. From JDK 1.2, it has been made to implement Map interface, making it a member of collection framework.
9) When To Use What?
HashMap is always recommended if you don’t want thread safety. If you want thread safety, use either ConcurrentHashMap or make HashMap thread safe by using external synchronization through Collections.synchronizedMap() method. HashTable is not always recommended to use as it is considered as a legacy class.
HashMap Vs HashTable In Java :
HashMap
|
HashTable
|
HashMap is not synchronized and therefore it is not thread safe.
|
HashTable is internally synchronized and therefore it is thread safe.
|
HashMap allows maximum one null key and any number of null values.
|
HashTable doesn’t allow null keys and null values.
|
Iterators returned by the HashMap are fail-fast in nature.
|
Enumeration returned by the HashTable are fail-safe in nature.
|
HashMap extends AbstractMap class.
|
HashTable extends Dictionary class.
|
HashMap returns only iterators to traverse.
|
HashTable returns both Iterator as well as Enumeration for traversal.
|
HashMap is fast.
|
HashTable is slow.
|
HashMap is not a legacy class.
|
HashTable is a legacy class.
|
HashMap is preferred in single threaded applications. If you want to use HashMap in multi threaded application, wrap it using Collections.synchronizedMap() method.
|
Although HashTable is there to use in multi threaded applications, now a days it is not at all preferred. Because, ConcurrentHashMap is better option than HashTable.
|
Similarities Between HashMap And HashTable In Java :
1) Both store the data in the form of key-value pairs.
2) Both use Hashing technique to store the key-value pairs.
3) Both implement Map interface.
4) Both doesn’t maintain any order for elements.
5) Both give constant time performance for insertion and retrieval operations.
post a comment