Difference between hashmap and hashtable
In this post, you will learn the difference between hashmap and hashtable using the Java programming language. Such a type of question is frequently asked in an interview or test.
Java Hashmap
Java HashMap class implements the Map interface, which allows us to store key and value pairs where keys should be unique. HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It allows us to store the null elements as well, but there should be only one null key. The HashMap is non synchronised and not thread safe. For non-threaded applications, HashMap should be preferred over Hashtable
Java Hashtable
Hashtable is synchronized. It is thread-safe. The Hashtable class implements a hash table, which maps keys to values. Hashtable doesn't allow any null key or value. The hashtable is synchronized, which means the hashtable is thread-safe and can be shared between multiple threads.
Difference between hashmap and hashtable
There are many differences between hashmap and hashtable. Here, we have mentioned most of them-
Differences | Hashmap | Hashtable |
Synchronization or Thread Safe | HashMap is non synchronised and not thread safe. | Hashtable is thread safe and synchronised. |
Null keys and null values | HashMap allows one null key and any number of null values. | Hashtable do not allow null keys and null values in the Hashtable object. |
Null keys and null values | HashMap object values are iterated by using iterator. | Hashtable is the only class other than vector which uses enumerator to iterate the values of Hashtable object. |
Example of Java HashMap
// Import the HashMap class
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Create a HashMap object called animal
HashMap<String, Integer> animal = new HashMap<String, Integer>();
// Add keys and values (Name, Age)
animal.put("Dog", 10);
animal.put("Cat", 20);
animal.put("Fish", 30);
for (String i : animal.keySet()) {
System.out.println("key: " + i + ", value: " + animal.get(i));
}
}
}
Output of the above code:
key: Cat, value: 20
key: Fish, value: 30
key: Dog, value: 10
Example of Java Hashtable
import java.util.*;
public class Hashtable1{
public static void main(String args[]){
Hashtable<Integer,String> people = new Hashtable<Integer,String>();
people.put(101,"Smith");
people.put(102,"Andy");
people.put(103,"Gaga");
people.put(104,"Michel");
for(Map.Entry m:people.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output of the above code:
104 Michel
103 Gaga
102 Andy
101 Smith
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java