Differences:
Both HashMap and HashTable does not guarantee that the order of the map will remain constant over time.
When to use HashMap and Hashtable?
1. Single Threaded Application
HashMap should be preferred over Hashtable for the non-threaded applications.
In simple words , use HashMap in unsynchronized or single threaded applications .
2. Multi Threaded Application
We should avoid using HashTable, as the class is now obsolete in latest Jdk 1.8.
Oracle has provided a better replacement of HashTable named ConcurrentHashMap.
For multithreaded application prefer ConcurrentHashMap instead of Hashtable. It would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
- HashMap allows null values as key and value whereas HashTable doesn't.maps.put(null, 7);maps.put(null, 15);The key is null and the value of 15 overwrite 7.
- HashMap is non synchronized whereas Hashtable is synchronized.
- HashTable is the only class other than vector which have method return enumerator to iterate The values.
- The iterator in HashMap is fail-fast iterator while the enumerator for Hashtable is not.
Both HashMap and HashTable does not guarantee that the order of the map will remain constant over time.
When to use HashMap and Hashtable?
1. Single Threaded Application
HashMap should be preferred over Hashtable for the non-threaded applications.
In simple words , use HashMap in unsynchronized or single threaded applications .
2. Multi Threaded Application
We should avoid using HashTable, as the class is now obsolete in latest Jdk 1.8.
Oracle has provided a better replacement of HashTable named ConcurrentHashMap.
For multithreaded application prefer ConcurrentHashMap instead of Hashtable. It would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
HashMap | HashTable | |
---|---|---|
Synchronized | No | Yes |
Thread-Safe | No | Yes |
Null Keys and Null values | One null key ,Any null values | Not permit null keys and values |
Iterator type | Fail fast iterator | Fail safe iterator |
Performance | Fast | Slow in comparision |
Superclass and Legacy | AbstractMap , No | Dictionary , Yes |
No comments:
Post a Comment