Home »
Java programming language
Differences between Hashtable and HashMap class in Java
Hashtable vs HashMap class in Java: Here, we are going to learn what are the differences between Hashtable and HashMap class in Java programming language?
Submitted by Preeti Jain, on August 07, 2019
HashMap vs HashSet
Here, we will see how Hashtable differs from HashMap in Java?
Hashtable class
- Hashtable is a class which is available in java.util package.
- In Hashtable, every method of the class is synchronized so that it provides Thread Safety.
- Hashtable is Thread Safe (i.e. only one thread is allowed to operate on Hashtable object at a time).
- In Hashtable one thread operates on Hashtable object at a time so it takes more time to complete the task or in other words, we can say it increases the waiting time of the thread.
- In the case of Hashtable, performance is low just because of taking the more waiting time of the thread.
- We can insert null for both keys and values.
- Hashtable is a legacy class because this class is introduced in earlier version 1.0 of Java so this class is the need to re-engineered to support collection when collection framework came.
- Hashtable does not provide uniqueness fully (i.e. duplicate are not allowed for keys and duplicates are allowed for values).
- In Hashtable insertion, the order is not preserved (i.e. insertion and retrieval order is not needed to be same).
Example:
import java.util.Hashtable;
class HashTableClass {
int hashcode;
// class constructor to instantiate hashcode
HashTableClass(int hashcode) {
this.hashcode = hashcode;
}
// override hashCode()
public int hashCode() {
return hashcode;
}
// override toString() for string conversion
public String toString() {
return hashcode + " ";
}
public static void main(String[] args) {
// Creating an instance
Hashtable ht = new Hashtable();
// By using put() to add few objects in Hashtable
ht.put(new HashTableClass(10), "Java");
ht.put(new HashTableClass(3), "C");
ht.put(new HashTableClass(4), "C++");
ht.put(new HashTableClass(3), "Ruby");
ht.put(new HashTableClass(5), "C");
ht.put(new HashTableClass(6), "null");
// Display Current Hashtable
System.out.println("Current Hashtable is :" + ht);
}
}
Output
E:\Programs>javac HashTableClass.java
E:\Programs>java HashTableClass
Current Hashtable is :{10 =Java, 6 =null, 5 =C, 4 =C++, 3 =Ruby, 3 =C}
Here, we will see how HashMap differs from Hashtable in Java?
HashMap
- HashMap is a class which is available in java.util package.
- In HashMap no method of the class is synchronized so that it does not provide Thread Safety.
- HashMap is not Thread Safe (i.e. multiple threads is allowed to operate on Hashtable object at a time).
- In HashMap multiple threads operate on Hashtable object at a time so it takes less time to complete the task or in other words, we can say it decrease the waiting time of the thread.
- In the case of HashMap performance is high just because of taking the less waiting time of the thread.
- We can insert null for both keys(once) and values(multiple times).
- HashMap is not a legacy class because this class is introduced in later version 1.2 of Java so this class does not need to re-engineered to support collection when collection framework came.
- HashMap does not provide uniqueness fully (i.e. duplicate are not allowed for keys and duplicates are allowed for values).
- In HashMap insertion order is not preserved (i.e. insertion and retrieval order is not needed to be same).
Example:
// Java program to demonstrate the behavior of Map
import java.util.Collection;
import java.util.HashMap;
class HashMapClass {
public static void main(String[] args) {
// Creating an instance of HashMap
HashMap hm = new HashMap();
// By using put() method to add some values in HashMap
hm.put("Java", 1000);
hm.put("C", 2000);
hm.put("C++", 3000);
hm.put("Ruby", 4000);
hm.put("Python", 1000);
hm.put("null", null);
hm.put("Django", null);
// Here we will not get any error but one null is accepted for keys
hm.put("null", 7000);
// Display retrieval order of Map
System.out.println("Current HashMap list is :" + hm);
// by using values() to find values of HashMap
Collection values = hm.values();
// Display Values of HashMap
System.out.println("Current HashMap Key values is :" + values);
}
}
Output
E:\Programs>javac HashMapClass.java
E:\Programs>java HashMapClass
Current HashMap list is :{Ruby=4000, C=2000, Django=null,
Python=1000, C++=3000, null=7000, Java=1000}
Current HashMap Key values is :[4000, 2000, null, 1000, 3000, 7000, 1000]