Getting Synchronized Map from Java HashMap Last Updated : 13 Jan, 2021 Comments Improve Suggest changes Like Article Like Report 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 backed by the specified map. // Get synchronized map using Collections.synchronizedMap() Map<Integer, String> synchrMap = Collections.synchronizedMap(hmap); To iterate the synchronized map we use a synchronized block: // Synchronized block synchronized (synchrMap) { // Iterate synchronized map for (Map.Entry<Integer, String> entry : synchrMap.entrySet()) { // Print key : value System.out.println(entry.getKey() + " : " + entry.getValue()); } } Implementation: Java // Java Program to demonstrate how // to get synchronized map from HashMap import java.util.*; class GFG { public static void main(String[] args) { // New HashMap HashMap<Integer, String> hmap = new HashMap<>(); // Add element to map hmap.put(1, "Akshay"); hmap.put(2, "Bina"); hmap.put(3, "Chintu"); // Get synchronized map using // Collections.synchronizedMap() Map<Integer, String> synchrMap = Collections.synchronizedMap(hmap); System.out.println("Synchronized Map : "); // Synchronized block synchronized (synchrMap) { // Iterate synchronized map for (Map.Entry<Integer, String> entry : synchrMap.entrySet()) { // Print key : value System.out.println(entry.getKey() + " : " + entry.getValue()); } } } } OutputSynchronized Map : 1 : Akshay 2 : Bina 3 : Chintu Comment More infoAdvertise with us Next Article Getting Synchronized Map from Java HashMap N nikhilchhipa9 Follow Improve Article Tags : Java Java Programs Java-Collections Java-HashMap Practice Tags : JavaJava-Collections Similar Reads Getting Synchronized Map from Java TreeMap TreeMap is a part of the Java Collections framework. Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order. Java TreeMap contains only unique elements. It cannot 2 min read Getting Set View of Keys from HashMap in Java The HashMap class of Java provides the functionality of the hash table data structure. This class is found in the java.util package. It implements the Map interface. It stores elements in (Key, Value) pairs and you can access them by an index of another type (e.g. Integer/String). Here, keys are use 2 min read Creating HashMap from Other Maps in Java Map interface present in java.util package represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore, it behaves a bit differently from the rest of the collection types. A map contains unique keys. There are three main types of maps in 3 min read Removing all Mapping From HashMap in Java Maps are used for when you want to associate a key with a value and Lists are an ordered collection. Map is an interface in the Java Collection Framework and a HashMap is one implementation of the Map interface. HashMap is efficient for locating a value based on a key and inserting and deleting valu 4 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 Like