Home »
Java programming language
Java Collections synchronizedSortedMap() Method with Example
Collections Class synchronizedSortedMap() method: Here, we are going to learn about the synchronizedSortedMap() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 07, 2020
Collections Class synchronizedSortedMap() method
- synchronizedSortedMap() method is available in java.util package.
- synchronizedSortedMap() method is used to return the synchronized view of the given sorted map (sm).
- synchronizedSortedMap() method is a static method, it is accessible with the class name and if we try to access the method with the class object then also we will not get any error.
- synchronizedSortedMap() method does not throw an exception at the time of returning synchronized sorted map.
Syntax:
public static SortedMap synchronizedSortedMap(SortedMap sm);
Parameter(s):
- SortedMap sm – represents the sorted map to be viewed in synchronized sorted map.
Return value:
The return type of this method is SortedMap, it returns synchronized view of the given sorted map.
Example:
// Java program to demonstrate the example
// of SortedMap synchronizedSortedMap() method
// of Collections
import java.util.*;
public class SynchronizedSortedMapOfCollections {
public static void main(String args[]) {
// Instantiates a sorted map object
SortedMap < Integer, String > tm = new TreeMap < Integer, String > ();
// By using put() method is to add
// objects in a treemap
tm.put(10, "C");
tm.put(20, "C++");
tm.put(30, "JAVA");
tm.put(40, "PHP");
tm.put(50, "SFDC");
// Display TreeMap
System.out.println("TreeMap: " + tm);
// By using synchronizedSortedMap() method is to
// represent the treemap in synchronized view
tm = Collections.synchronizedSortedMap(tm);
// Display Synchronized SortedMap
System.out.println("Collections.synchronizedSortedMap(tm): " + tm);
}
}
Output
TreeMap: {10=C, 20=C++, 30=JAVA, 40=PHP, 50=SFDC}
Collections.synchronizedSortedMap(tm): {10=C, 20=C++, 30=JAVA, 40=PHP, 50=SFDC}