Collections synchronizedMap() method in Java with Examples Last Updated : 06 May, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report The synchronizedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map. Syntax: public static <K, V> Map<K, V> synchronizedMap(Map<K, V> m) Parameters: This method takes the map as a parameter to be "wrapped" in a synchronized map. Return Value: This method returns a synchronized view of the specified map. Below are the examples to illustrate the synchronizedMap() method. Example 1: Java // Java program to demonstrate // synchronizedMap() method // for <String, String> Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Map<String, String> Map<String, String> map = new HashMap<String, String>(); // populate the map map.put("Value1", "20"); map.put("Value2", "30"); map.put("Value3", "40"); // printing the Collection System.out.println("Map : " + map); // create a synchronized map Map<String, String> synmap = Collections.synchronizedMap(map); // printing the Collection System.out.println("Synchronized map is : " + synmap); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } Output: Map : {Value3=40, Value1=20, Value2=30} Synchronized map is : {Value3=40, Value1=20, Value2=30} Example 2: Java // Java program to demonstrate // synchronizedMap() method // for <String, Boolean> Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Map<String, Boolean> Map<String, Boolean> map = new HashMap<String, Boolean>(); // populate the map map.put("Bramha", true); map.put("Vishnu", true); map.put("Mahesh", true); // printing the Collection System.out.println("Map : " + map); // create a synchronized map Map<String, Boolean> synmap = Collections.synchronizedMap(map); // printing the Collection System.out.println("Synchronized map is : " + synmap); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } Output: Map : {Bramha=true, Vishnu=true, Mahesh=true} Synchronized map is : {Bramha=true, Vishnu=true, Mahesh=true} Comment More infoAdvertise with us Next Article Collections singletonMap() method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Collections synchronizedSortedMap() method in Java with Examples The synchronizedSortedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) sorted map backed by the specified sorted map. In order to guarantee serial access, it is critical that all access to the backing sorted map is accomplished through the returned sorted ma 2 min read Collections singletonMap() method in Java with Examples The singletonMap() method of java.util.Collections class is used to return an immutable map, mapping only the specified key to the specified value. The returned map is serializable. Syntax: public static Map singletonMap(K key, V value) Parameters: This method takes the following parameters as a arg 2 min read Collections newSetFromMap() method in Java with Examples The newSetFromMap() method of java.util.Collections class is used to return a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to 2 min read Collections unmodifiableSortedMap() method in Java with Examples The unmodifiableSortedMap() method of java.util.Collections class is used to return an unmodifiable view of the specified sorted map. This method allows modules to provide users with "read-only" access to internal sorted maps. Query operations on the returned sorted map "read through" to the specifi 2 min read SortedMap clear() method in Java with Examples The clear() method in SortedMap Java is used to clear and remove all of the elements or mappings from a specified SortedMap collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Note: The clear() method in SortedMa 2 min read SortedMap clear() method in Java with Examples The clear() method in SortedMap Java is used to clear and remove all of the elements or mappings from a specified SortedMap collection. Syntax: void clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Note: The clear() method in SortedMa 2 min read Like