📜  Java中的集合 checkedSortedSet() 方法及示例

📅  最后修改于: 2022-05-13 01:54:18.405000             🧑  作者: Mango

Java中的集合 checkedSortedSet() 方法及示例

Java.util.Collections类的checkedSortedSet()方法用于返回指定排序集的动态类型安全视图。
如果指定的排序集是可序列化的,则返回的排序集将是可序列化的。
由于 null 被认为是任何引用类型的值,因此返回的排序集允许在支持排序集插入时插入 null 元素。
句法:

public static  SortedSet checkedSortedSet(SortedSet s, Class type)

参数:此方法将以下参数作为参数:

  • s –要为其返回动态类型安全视图的排序集
  • type –允许 s 持有的元素的类型

返回值:此方法返回指定排序集的动态类型安全视图
以下是说明checkedSortedSet()方法的示例
示例 1:

Java
// 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
            SortedSet sset = new TreeSet();
 
            // Adding element 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
                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);
        }
    }
}


Java
// 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
            SortedSet sset = new TreeSet();
 
            // Adding element 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 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);
        }
    }
}


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

示例 2:

Java

// 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
            SortedSet sset = new TreeSet();
 
            // Adding element 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 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);
        }
    }
}
输出:
Sorted Set: [20, 30, 40]
Typesafe view of sorted set: 
[20, 30, 40]