📜  Java中的集合checkedSet()方法和示例

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

Java中的集合checkedSet()方法和示例

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

public static  Set checkedSet(Set s, Class type)

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

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

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

Java
// Java program to demonstrate
// checkedSet() method
// for String value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
 
            // creating object of Set
            Set hset = new TreeSet();
 
            // Adding element to hmap
            hset.add("Ram");
            hset.add("Gopal");
            hset.add("Verma");
 
            // print the set
            System.out.println("Set: " + hset);
 
            // create typesafe view of the specified set
            Set
                tsset = Collections
                            .checkedSet(hset, String.class);
 
            // printing the typesafe view of specified list
            System.out.println("Typesafe view of Set: "
                               + tsset);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// checkedSet() method
// for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of Set
            Set hset = new TreeSet();
 
            // Adding element to hset
            hset.add(20);
            hset.add(30);
            hset.add(40);
 
            // print the set
            System.out.println("Set: " + hset);
 
            // create typesafe view of the specified set
            Set
                tsset = Collections
                            .checkedSet(hset, Integer.class);
 
            // printing the typesafe view of specified list
            System.out.println("Typesafe view of Set: " + tsset);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


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

示例 2:

Java

// Java program to demonstrate
// checkedSet() method
// for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // creating object of Set
            Set hset = new TreeSet();
 
            // Adding element to hset
            hset.add(20);
            hset.add(30);
            hset.add(40);
 
            // print the set
            System.out.println("Set: " + hset);
 
            // create typesafe view of the specified set
            Set
                tsset = Collections
                            .checkedSet(hset, Integer.class);
 
            // printing the typesafe view of specified list
            System.out.println("Typesafe view of Set: " + tsset);
        }
        catch (IllegalArgumentException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Set: [20, 30, 40]
Typesafe view of Set: [20, 30, 40]