How to Create a Synchronized HashTable in Java?
Last Updated :
21 Feb, 2024
In Java, a synchronized HashTable is achieved by wrapping a regular HashTable with the Collection.synchronizedMap( ) method. This wrapper ensures that each method of the Map interface is synchronized, making the HashTable thread-safe.
Syntax:
Map<KeyType, ValueType> synchronizedHashTable = Collections.synchronizedMap(new HashTable<>());
Parameters:
- keyType: The type of keys in the HashTable.
- valueType: The type of values in the HashTable.
Approaches:
- Use the Collections.synchronizedMap( ) method to wrap a regular HashTable.
- Ensure that all read and write operations on the HashTable are performed using the synchronized wrapper.
Program to Create a Synchronized HashTable in Java
Below are the code implementations using the above two approaches.
Approach 1: Using Collections.synchronizedMap()
Below is the Program to create a synchronized HashTable using Collections.synchronizedMap().
Java
import java.io.*;
import java.util.*;
class SynchronizedHashTable {
public static void main (String[] args) {
Map<String, Integer> hashTable = new HashMap();
Map<String, Integer> synchronizedHashTable = Collections.synchronizedMap(hashTable);
synchronizedHashTable.put( "one" , 1 );
synchronizedHashTable.put( "Two" , 2 );
synchronizedHashTable.put( "Three" , 3 );
synchronized (synchronizedHashTable) {
for (Map.Entry<String, Integer> entry : synchronizedHashTable.entrySet()) {
System.out.println( "Synchronized HashTable is: " + entry.getKey() + ": " + entry.getValue());
}
}
}
}
|
Output
Synchronized HashTable is: one: 1
Synchronized HashTable is: Two: 2
Synchronized HashTable is: Three: 3
Explanation of the above Program:
- We have created a
HashMap
named hashTable
.
- We have used the
Collections.synchronizedMap
method to create a synchronized version of hashTable
named synchronizedHashTable
.
- Then we performed operations like
put
on the synchronizedHashTable
.
- After that we iterate over the
synchronizedHashTable
using the enhanced for loop.
- At last, it prints the key-value pairs from the synchronized hash table.
Approach 2: Using Concurrent Class
Below is the Program to Create a Synchronized HashTable using Concurrent Class
Java
import java.util.*;
import java.util.concurrent.*;
public class ConcurrentHashTable {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> concurrentHashTable = new ConcurrentHashMap<>();
concurrentHashTable.put( "one" , 1 );
concurrentHashTable.put( "two" , 2 );
concurrentHashTable.put( "three" , 3 );
for (Map.Entry<String, Integer> entry : concurrentHashTable.entrySet()) {
System.out.println( "Synchronized HashTable is: " + entry.getKey() + ": " + entry.getValue());
}
}
}
|
Output
Synchronized HashTable is: one: 1
Synchronized HashTable is: two: 2
Synchronized HashTable is: three: 3
Explanation of the above Program:
- First, we have created a
ConcurrentHashMap
named concurrentHashTable
.
- Then we perform operations like
put()
method
on the concurrentHashTable
.
- After that we iterate over the
concurrentHashTable
using the enhanced for loop.
- At last, it prints the key-value pairs from the concurrent hash table.
Share your thoughts in the comments
Please Login to comment...