How to Create a Synchronized HashTable in Java? Last Updated : 21 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 JavaBelow 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()); } } } } OutputSynchronized 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 ClassBelow 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()); } } } OutputSynchronized 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. Comment More infoAdvertise with us Next Article How to Create a Synchronized HashTable in Java? J javvajisunil16 Follow Improve Article Tags : Java Java Programs Java-HashTable Java Examples Practice Tags : Java Similar Reads How to Eliminate Duplicate Keys in Hashtable in Java? HashTable class is part of the Collection framework in Java where the only major difference it has from HashMap is that it's synchronized. Hash table maps keys to values i.e. it internally uses buckets to store key-value pairs and the corresponding bucket to a key-value pair is determined by the key 4 min read How to Iterate Through HashTable in Java? HashTable is an underlying data structure where the insertion order in HashTable is not preserved, and it is based on the hashcode of keys. Duplicates keys are not allowed, but values can be duplicated. Heterogeneous objects are allowed for both keys and values. Value null is not allowed for both ke 8 min read How to Convert a HashTable to Other Collections Types in Java? In Java, a Hashtable is a data structure that stores the data in the form of key and value pairs. And each key is mapped to a specific value. It implements the Map interface. HashTable provides a simple way to access the data using the unique keys. In this article, we will learn how to convert a Has 3 min read Create Thread-safe HashMap Without Using Collections.synchronizedMap in Java The HashMap class in Java is a commonly used data structure for storing key-value pairs. While HashMap provides fast and efficient operations, it is not inherently thread-safe. When working in a multi-threaded environment, it's crucial to ensure that data structures are accessed and modified in a wa 4 min read Getting Synchronized Map from Java HashMap HashMap is a non synchronized collection class. If we want to perform thread-safe operations on it then we must have to synchronize it explicitly. In order to synchronize it explicitly the synchronizedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) map back 2 min read Like