Collections checkedSortedSet() method in Java with Examples

The checkedSortedSet() method of java.util.Collections class is used to return a dynamically typesafe view of the specified sorted set.

The returned sorted set will be serializable if the specified sorted set is serializable.

Since null is considered to be a value of any reference type, the returned sorted set permits insertion of null elements whenever the backing sorted set does.

Syntax:

public static  SortedSet checkedSortedSet(SortedSet s, Class type)

Parameters: This method takes the following argument as a parameter:

  • s – the sorted set for which a dynamically typesafe view is to be returned
  • type – the type of element that s is permitted to hold

Return Value: This method returns a dynamically typesafe view of the specified sorted set.

Below are the examples to illustrate the checkedSortedSet() method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// checkedSortedSet() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of SortedMap<String>
            SortedSet<String> sset = new TreeSet<String>();
  
            // Adding elemnet to smap
            sset.add("Ram");
            sset.add("Gopal");
            sset.add("Verma");
  
            // printing the sorted set
            System.out.println("Sorted Set: " + sset);
  
            // create typesafe view of the specified set
            // using checkedSortedSet() method
            SortedSet<String>
                tsset = Collections
                            .checkedSortedSet(sset, String.class);
  
            // printing the typesafe view of specified set
            System.out.println("Typesafe view of sorted set: \n"
                               + tsset);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Sorted Set: [Gopal, Ram, Verma]
Typesafe view of sorted set: 
[Gopal, Ram, Verma]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// checkedSortedSet() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of SortedSet<Integer>
            SortedSet<Integer> sset = new TreeSet<Integer>();
  
            // Adding elemnet to smap
            sset.add(20);
            sset.add(30);
            sset.add(40);
  
            // printing the sorted set
            System.out.println("Sorted Set: " + sset);
  
            // create typesafe view of the specified set
            // using checkedSortedSet() method
            SortedSet<Integer> tsset = Collections.checkedSortedSet(sset, Integer.class);
  
            // printing the typesafe view of specified set
            System.out.println("Typesafe view of sorted set: \n"
                               + tsset);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Sorted Set: [20, 30, 40]
Typesafe view of sorted set: 
[20, 30, 40]


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.