Skip to content
Related Articles

Related Articles

Improve Article

Collections synchronizedSortedMap() method in Java with Examples

  • Last Updated : 08 Oct, 2018

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}

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :