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 map (or its views).

Syntax:

public static <K, V> SortedMapK, V>
  synchronizedSortedMap(SortedMapK, V> m)

Parameters: This method takes the sorted map as a parameter to be “wrapped” in a synchronized sorted map.

Return Value: This method returns a synchronized view of the specified sorted map.

Below are the examples to illustrate the synchronizedSortedMap() method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// synchronizedSortedMap() method
// for <String, String> Value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of SortedMap<String, String>
            SortedMap<String, String>
                map = new TreeMap<String, String>();
  
            // populate the map
            map.put("1", "A");
            map.put("2", "B");
            map.put("3", "C");
  
            // printing the Collection
            System.out.println("Sorted Map : " + map);
  
            // create a sorted map
            SortedMap<String, String>
                sortedmap = Collections
                                .synchronizedSortedMap(map);
  
            // printing the map
            System.out.println("Synchronized sorted map is :"
                               + sortedmap);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Sorted Map : {1=A, 2=B, 3=C}
Synchronized sorted map is :{1=A, 2=B, 3=C}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// synchronizedSortedMap() method
// for <Integer, Boolean> Value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of SortedMap<Integer, Boolean>
            SortedMap<Integer, Boolean>
                map = new TreeMap<Integer, Boolean>();
  
            // populate the map
            map.put(100, true);
            map.put(200, true);
            map.put(300, true);
  
            // printing the Collection
            System.out.println("Sorted Map : " + map);
  
            // create a sorted map
            SortedMap<Integer, Boolean>
                sortedmap = Collections
                                .synchronizedSortedMap(map);
  
            // printing the map
            System.out.println("Synchronized sorted map is :"
                               + sortedmap);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Sorted Map : {100=true, 200=true, 300=true}
Synchronized sorted map is :{100=true, 200=true, 300=true}


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.