Open In App

Collections synchronizedSortedMap() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

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

Example 2:




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


Output:

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


Last Updated : 08 Oct, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads