In this article, we will discuss difference between HashMap and Hashtable classes in detail i.e.; HashMap v/s Hashtable
1. Key points about Hashtable:
Hashtable is legacy class and based on hashcode of keys where keys are unique and it is exactly same as HashMap with few differences with respect to following points
- Synchronized methods
- Performance
- Null key insertion
- Null value insertion
Lets us move on and discuss key differences between these 2 Map implemented classes
2. HashMap v/s Hashtable:
HashMap | Hashtable |
HashMap is introduced in collection framework in Java 1.2 version | Hashtable is a legacy class and introduced in Java 1.0 version |
HashMap is NOT synchronized | Hashtable is synchronized |
All methods of HashMap is NOT synchronized i.e.; it is not thread-safe | All methods of HashMap is synchronized i.e.; thread-safe |
Multiple threads are allowed to access | Only one thread is allowed access; other threads has to wait to get access, after obtaining lock/monitor |
Performance-wise, this is relatively high comparing with Hashtable, as there is no wait time | Performance-wise, this is relatively slow due synchronized methods as there is only one thread allowed to access, at any given point of time |
NULL insertion allowed for both keys and values | NULL insertion is not allowed for both keys and values |
Maximum of one NULL key and there is no upper limit for values | Simply, not allowed for both keys & values |
Note: both uses hash table data structure to store key-value pairs
Q) When to use HashMap ?
- HashMap stores key-value pairs which uses hashing technique to store key-value pairs where methods are NOT synchronized
- So, search operation is faster with multiple threads access
- So, if business requirement is to store key-value pairs for faster search operation or more number of search operation on the basis of keys; without concerning concurrent access of map
- Then, HashMap is the very apt choice
Q) When to use Hashtable ?
- This is exactly same as that of HashMap, but every methods is synchronized
- Performance-wise is relatively slower than comparing HashMap
- So, if business requirement is to store key-value pairs for faster search operation with synchronized access
- Then, Hashtable is preferred choice over HashMap
3. Examples on HashMap and Hashtable:
3.1 Program on HashMap :
DemoHashMap.java
package in.bench.resources.collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DemoHashMap {
public static void main(String[] args) {
// 1. Creating HashMap object
Map<Integer, String> hashMap =
new HashMap<Integer, String>();
// 1.1 add few entries
hashMap.put(40, "RajKumarHirani");
hashMap.put(50, "SanjayLeelaBanshali");
hashMap.put(20, "Shankar");
hashMap.put(10, "ManiRatnam");
hashMap.put(30, "RajaMouli");
hashMap.put(null, null);
// 1.2 get entrySet()
Set<Map.Entry<Integer, String>> entries = hashMap.entrySet();
System.out.println("Displaying HashMap entries"
+ " in Random-order : \n");
// 1.3 Iterate using enhanced for-Each loop
for(Map.Entry<Integer, String> entry : entries) {
System.out.println("Rank : " + entry.getKey()
+ "\t Director : " + entry.getValue());
}
}
}
Output:
Displaying HashMap entries in Random-order :
Rank : null Director : null
Rank : 50 Director : SanjayLeelaBanshali
Rank : 20 Director : Shankar
Rank : 40 Director : RajKumarHirani
Rank : 10 Director : ManiRatnam
Rank : 30 Director : RajaMouli
4.2 Program on Hashtable :
DemoHashtable.java
package in.bench.resources.collection;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
public class DemoHashtable {
public static void main(String[] args) {
// 1. Creating Hashtable object
Map<Integer, String> hashtable =
new Hashtable<Integer, String>();
// 1.1 add few entries
hashtable.put(40, "RajKumarHirani");
hashtable.put(20, "Shankar");
hashtable.put(10, "ManiRatnam");
hashtable.put(50, "SanjayLeelaBanshali");
hashtable.put(30, "RajaMouli");
// 1.2 get entrySet()
Set<Map.Entry<Integer, String>> entries = hashtable.entrySet();
System.out.println("Displaying Hashtable entries"
+ " in Random-order : \n");
// 1.3 Iterate using enhanced for-Each loop
for(Map.Entry<Integer, String> entry : entries) {
System.out.println("Rank : " + entry.getKey()
+ "\t Director : " + entry.getValue());
}
}
}
Output:
Displaying Hashtable entries in Random-order :
Rank : 10 Director : ManiRatnam
Rank : 20 Director : Shankar
Rank : 30 Director : RajaMouli
Rank : 40 Director : RajKumarHirani
Rank : 50 Director : SanjayLeelaBanshali
Related Articles:
- Map interface
- Entry interface
- HashMap class
- LinkedHashMap class
- IdentityHashMap class
- WeakHashMap class
- SortedMap interface
- NavigableMap interface
- TreeMap class
- Hashtable class
- HashMap vs LinkedHashMap
- HashMap v/s LinkedHashMap v/s TreeMap
- HashMap v/s HashSet
- HashMap v/s Hashtable
- Properties class
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html
Happy Coding !!
Happy Learning !!