Collections synchronizedMap() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Map : {Value3=40, Value1=20, Value2=30}
Synchronized map is : {Value3=40, Value1=20, Value2=30}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Map : {Bramha=true, Vishnu=true, Mahesh=true}
Synchronized map is : {Bramha=true, Vishnu=true, Mahesh=true}

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ytxmobile