Horje
How to Create a Synchronized HashTable in Java?

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

// Java program to Create a synchronized HashTable
// Using Collections.synchronizedMap( )
import java.io.*;
import java.util.*;
  
class SynchronizedHashTable {
    public static void main (String[] args) {
      // Create a hash table
      Map<String, Integer> hashTable = new HashMap();
        
      // Wrap the hash table with a synchronized map
      Map<String, Integer> synchronizedHashTable = Collections.synchronizedMap(hashTable);
        
      // Perform operations on the synchronized hash table
      synchronizedHashTable.put("one", 1);
      synchronizedHashTable.put("Two", 2);
      synchronizedHashTable.put("Three",3);
        
      // Iterate over the synchronized hash table
      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

// Java program to create a synchronized HashTable
// Using concurrent class
import java.util.*;
import java.util.concurrent.*;
   
public class ConcurrentHashTable {
    public static void main(String[] args) {
        // Create a concurrent Hash Table
        ConcurrentHashMap<String, Integer> concurrentHashTable = new ConcurrentHashMap<>();
   
        // Perform operations on the concurrent Hash Table
        concurrentHashTable.put("one", 1);
        concurrentHashTable.put("two", 2);
        concurrentHashTable.put("three", 3);
   
        // Iterate over the concurrent hash table
        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.



Reffered: https://www.geeksforgeeks.org


Java

Related
How to Handle SQLException in JDBC? How to Handle SQLException in JDBC?
How to do Method Overloading for Null Argument? How to do Method Overloading for Null Argument?
How to Find Which Week of the Year in Java? How to Find Which Week of the Year in Java?
How to Perform Java Parallel Matrix Multiplication? How to Perform Java Parallel Matrix Multiplication?
How to Merge Two HashMaps in Java while Handling Conflicts? How to Merge Two HashMaps in Java while Handling Conflicts?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16