Collections synchronizedSortedMap() method in Java with Examples Last Updated : 08 Oct, 2018 Comments Improve Suggest changes Like Article Like 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 // 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 // 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} Comment More infoAdvertise with us Next Article Collections synchronizedSortedMap() 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 synchronizedSortedSet() method in Java with Examples The synchronizedSortedSet() method of java.util.Collections class is used to return a synchronized (thread-safe) sorted set backed by the specified sorted set. In order to guarantee serial access, it is critical that all access to the backing sorted set is accomplished through the returned sorted se 2 min read 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 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 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 SortedMap size() method in Java with Examples The size() method of SortedMap interface in Java is used to get the size of the SortedMap which refers to the number of the key-value pair or mappings in the SortedMap. Syntax: int size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the SortedMap 2 min read Like