VOOZH about

URL: https://www.geeksforgeeks.org/java/collections-checkedsortedmap-method-in-java-with-examples/

⇱ Collections checkedSortedMap() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collections checkedSortedMap() method in Java with Examples

Last Updated : 6 Jun, 2021

The checkedSortedMap() method of java.util.Collections class is used to return a dynamically typesafe view of the specified sorted map.
The returned map will be serializable if the specified map is serializable.
Since null is considered to be a value of any reference type, the returned map permits insertion of null keys or values whenever the backing map does.
Syntax: 
 

public static SortedMap
 checkedSortedMap(SortedMap m, Class keyType, Class valueType)


Parameters: This method takes the following argument as a parameter 
 

  • m - the map for which a dynamically typesafe view is to be returned
  • keyType - the type of key that m is permitted to hold
  • valueType - the type of value that m is permitted to hold


Return Value: This method returns a dynamically typesafe view of the specified map.
Below are the examples to illustrate the checkedSortedMap() method
Example 1: 
 


Output: 
Sorted map:
{Karan=Arjun, Karn=Veer, Ram=Gopal}
Typesafe view of sorted map:
{Karan=Arjun, Karn=Veer, Ram=Gopal}

 

Example 2: 
 


Output: 
Sorted map:
{Karan=39, Karn=40, Ram=20}
Typesafe view of sorted map:
{Karan=39, Karn=40, Ram=20}

 
Comment