Browsed by
Tag: Collections in Java

How can we make Hashmap Synchronized

How can we make Hashmap Synchronized

HashMap can be synchronized by Map m = Collections.synchronizedMap(hashMap); In Core Java, you can make a HashMap synchronized by using the Collections.synchronizedMap() method. This method returns a synchronized (thread-safe) map backed by the specified map. Here’s an example: java import java.util.Collections; import java.util.HashMap; import java.util.Map;public class SynchronizedHashMapExample { public static void main(String[] args) { // Creating a HashMap Map<String, Integer> hashMap = new HashMap<>(); // Populating the HashMap hashMap.put(“One”, 1); hashMap.put(“Two”, 2); hashMap.put(“Three”, 3); // Making the HashMap synchronized Map<String, Integer>…

Read More Read More