
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between HashTable and ConcurrentHashMap in Java
In this article, we will learn about the ConcurrentHashMap and HashTable in Java. First, we will know about ConcurrentHashMap, the syntax of ConcurrentHashMap, and an example after that will learn about HashTable, the syntax of HashTable, and an example. At the end, we will see a table showing the difference between ConcurrentHashMap and HashTable.
What is ConcurrentHashMap in Java?
ConcurrentHashMap is a class that was introduced in JDK 1.5. The ConcurrentHashMap is threadsafe as it allows multiple threads to read and write the data without locking the entire map. ConcurrentHashMap applies locks only at the bucket level, called a fragment, while adding or updating the map. So, a ConcurrentHashMap allows concurrent read and write operations to the map.
Syntax for declaring the ConcurrentHashMap
ConcurrentHashMap<KeyType, ValueType> map = new ConcurrentHashMap<>();
Example of ConcurrentHashMap
Below is an example to show thread-safe usage of ConcurrentHashMap:
import java.util.ArrayList; import java.util.EnumMap; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapExample { public static void main(String[] args) { // ConcurrentHashMap Map<String, String> myMap = new ConcurrentHashMap<>(); myMap.put("HCL", "1"); myMap.put("DELL", "1"); // print the map for (Map.Entry m : myMap.entrySet()) { System.out.println(m.getKey() + " " + m.getValue()); } } }
What is HashTable in Java?
HashTable is a thread-safe legacy class introduced in the Jdk1.1 which is one of the oldest collection classes present in Java. It is a base implementation of the Map interface. It doesn't allow null keys and values. It is synchronized in nature, so two different threads can't access it simultaneously. Hashtable does not maintain any order.
Syntax for declaring the Hashtable
Hashtable <String, numbers> numbers = new Hashtable <String, numbers>();
Example of a Hashtable
Below is an example to show thread-safe usage of Hashtable:
import java.util.ArrayList; import java.util.EnumMap; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Map; public class HashtableExample { public static void main(String[] args) { // create Hashtable Hashtable<String, String> map = new Hashtable<>(); map.put("HCL", "100"); map.put("DELL", "200"); map.put("IBM", "300"); // print the map for (Map.Entry<String, String> m : map.entrySet()) { System.out.println(m.getKey() + " " + m.getValue()); } } }
Difference between HashTable and ConcurrentHashMap
Below is the comparison table of HashTable and ConcurrentHashMap:
Sr. No. |
Key |
HashTable |
ConcurrentHashMap |
---|---|---|---|
1 |
Basic |
HashTable is a thread-safe legacy class introduced in the Jdk1.1 |
ConcurrentHashmap is a class that was introduced in JDK 1.5 |
2 |
Locking |
It applies lock on the entire collection |
ConcurrentHashMap apply locks only at bucket level called fragment while adding or updating the map |
3 |
Performance |
It is slower than ConcurrentHashMap |
It is better than HashTable |
4. |
Null |
It doesn't allow null key and value |
It allows null key and value |
5 | How to achieve thread safety? | Method-level synchronization. |
By using bucket-level locking and non-blocking algorithms |