Java 中的 Collections checkedList()方法,带示例

原文:https://www . geesforgeks . org/collections-checked list-method-in-Java-with-examples/

Collections 类checkedList() 方法存在于 java.util 包中,用于返回指定列表的动态类型安全视图。这里需要注意的关键是,如果指定的列表是可序列化的,那么返回的列表将是可序列化的。因为 null 被认为是任何引用类型的值,所以返回的列表允许在后备列表允许的情况下插入 null 元素。

提示:该方法与 java 及更高版本兼容。

语法:

public static  List
checkedList(List list, Class type)

参数:该方法采用以下参数作为参数:

  • 要为其返回动态类型安全视图的列表
  • 允许列表包含的元素类型

返回类型:指定列表的动态类型安全视图

异常:此方法抛出类异常

例 1:

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to Demonstrate checkedList() method
// of Collections class for a string value

// Importing required 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 ArrayList of string type by
            // declaring object of List
            List<String> arlst = new ArrayList<String>();

            // Adding element to ArrayList
            // by using standard add() method

            // Custom input elements
            arlst.add("A");
            arlst.add("B");
            arlst.add("C");
            arlst.add("TajMahal");

            // Printing the above elements inside ArrayList
            System.out.println("List: " + arlst);

            // Creating typesafe view of the specified list
            // and applying checkedList
            List<String> tslst = Collections.checkedList(
                arlst, String.class);

            // Printing the updated elements of ArrayList
            // after applying above operation
            System.out.println("Typesafe view of List: "
                               + tslst);
        }

        // Catch block to handle the exceptions
        catch (IllegalArgumentException e) {

            // Display message on console if exception
            // occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output: 

List: [A, B, C, TajMahal]
Typesafe view of List: [A, B, C, TajMahal]

例 2:

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to Demonstrate checkedList() method
// of Collections class for a string value

// Importing required 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 of integer type
            // by creating an object of List
            List<Integer> arlst = new ArrayList<Integer>();

            // Adding element to above ArrayList
            // by using add() method
            arlst.add(20);
            arlst.add(30);
            arlst.add(40);
            arlst.add(50);

            // Printing the elements of above ArrayList
            System.out.println("List: " + arlst);

            // Creating typesafe view of the specified list
            // with usage of checkedList() method
            List<Integer> tslst = Collections.checkedList(
                arlst, Integer.class);

            // Printing the elements of ArrayList
            // after performing above operation
            System.out.println("Typesafe view of List: "
                               + tslst);
        }

        // Catch block to handle the exceptions
        catch (IllegalArgumentException e) {

            // Display message if exception occurs
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output: 

List: [20, 30, 40, 50]
Typesafe view of List: [20, 30, 40, 50]