Collections checkedSortedMap() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// checkedSortedMap() method
// for <String, String> type
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
  
            // creating object of SortedMap<String, String>
            SortedMap<String, String>
                smap = new TreeMap<String, String>();
  
            // Adding elemnet to smap
            smap.put("Ram", "Gopal");
            smap.put("Karan", "Arjun");
            smap.put("Karn", "Veer");
  
            // printing the sorted map
            System.out.println("Sorted map:\n"
                               + smap);
  
            // create typesafe view of the specified map
            SortedMap<String, String>
                tsmap = Collections
                            .checkedSortedMap(smap,
  
<strong>Output:</strong>
<pre>{Karan=39, Karn=40, Ram=20}</pre>
                                              String.class,
                                              String.class);
  
            // printing the typesafe view of specified sorted map
            System.out.println("Typesafe view of sorted map:\n"
                               + tsmap);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// checkedSortedMap() method
// for <String, Integer> type
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of SortedMap<String, Integer>
            SortedMap<String, Integer> smap = new TreeMap<String, Integer>();
  
            // Adding elemnet to smap
            smap.put("Ram", 20);
            smap.put("Karan", 39);
            smap.put("Karn", 40);
            // printing the sorted map
            System.out.println("Sorted map:\n"
                               + smap);
  
            // create typesafe view of the specified map
            SortedMap<String, Integer>
                tsmap = Collections
                            .checkedSortedMap(smap,
                                              String.class,
                                              Integer.class);
  
            // printing the typesafe view of specified sorted map
            System.out.println("Typesafe view of sorted map:\n"
                               + tsmap);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.