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

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

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

Java.util.Collections 类的checkedCollection()方法用于返回指定集合的动态类型安全视图。返回的集合不会将 hashCode 和 equals 操作传递给支持集合,而是依赖于 Object 的 equals 和 hashCode 方法。在支持集合是集合或列表的情况下,这对于保留这些操作的合同是必要的。如果指定的集合是可序列化的,则返回的集合将是可序列化的。

由于 null 被认为是任何引用类型的值,因此返回的集合允许在支持集合时插入 null 元素。

句法:

public static  Collection 
checkedCollection(Collection c, Class type)

参数:该方法有两个参数

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

返回类型:此方法返回指定集合的动态类型安全视图

示例 1:

Java
// Java program to demonstrate
// checkedCollection() method
// for String value
 
// Importing utility classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an empty ArrayList by
            // creating object of List of string type
            List arlst = new ArrayList();
 
            // Adding element to arrlist
            // using add() method
 
            // Custom input elements
            arlst.add("CSS");
            arlst.add("PHP");
            arlst.add("HTML");
            arlst.add("TajMahal");
 
            // Printing the ArrayList
            System.out.println("List: " + arlst);
 
            // Now create typesafe view of the collection bu
            // creating object of Collection class of string
            // type
            Collection tslst
                = Collections.checkedCollection(
                    arlst, String.class);
 
            // Printing the updated ArrayList
            // after applying checkedCollection() method
            System.out.println("Typesafe view of List: "
                               + tslst);
        }
 
        // Catch block to handle the exceptions
        catch (IllegalArgumentException e) {
 
            // Print message to be displayed on console
            // when exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to Illustrate checkedCollection() method
// of Collection class for an Integer value
 
// Importing classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exception
        try {
 
            // Creating an empty ArrayList of integer type
            // by declaring object of List
            List arlst = new ArrayList();
 
            // Adding element to ArrayList
            // using add() method
            arlst.add(20);
            arlst.add(30);
            arlst.add(40);
            arlst.add(50);
 
            // Printing the above ArrayList
            System.out.println("List: " + arlst);
 
            // now creating typesafe view of the collection
            // using checkedCollection() method
            Collection tslst
                = Collections.checkedCollection(
                    arlst, Integer.class);
 
            // Printing the updated ArrayList
            // after above operation
            System.out.println("Typesafe view of List: "
                               + tslst);
        }
 
        // Catch block to handle exceptions
        catch (IllegalArgumentException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
List: [CSS, PHP, HTML, TajMahal]
Typesafe view of List: [CSS, PHP, HTML, TajMahal]

示例 2:

Java

// Java program to Illustrate checkedCollection() method
// of Collection class for an Integer value
 
// Importing classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv) throws Exception
    {
 
        // Try block to check for exception
        try {
 
            // Creating an empty ArrayList of integer type
            // by declaring object of List
            List arlst = new ArrayList();
 
            // Adding element to ArrayList
            // using add() method
            arlst.add(20);
            arlst.add(30);
            arlst.add(40);
            arlst.add(50);
 
            // Printing the above ArrayList
            System.out.println("List: " + arlst);
 
            // now creating typesafe view of the collection
            // using checkedCollection() method
            Collection tslst
                = Collections.checkedCollection(
                    arlst, Integer.class);
 
            // Printing the updated ArrayList
            // after above operation
            System.out.println("Typesafe view of List: "
                               + tslst);
        }
 
        // Catch block to handle exceptions
        catch (IllegalArgumentException e) {
 
            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
List: [20, 30, 40, 50]
Typesafe view of List: [20, 30, 40, 50]