Java Collections checkedSet() 方法



描述

Java Collections checkedSet(Set<E>, Class<E>) 方法用於獲取指定集合的動態型別安全檢視。

宣告

以下是 java.util.Collections.checkedSet() 方法的宣告。

public static <E> Set<E> checkedSet(Set<E> s, Class<E> type)

引數

  • s − 這是要返回動態型別安全檢視的集合。

  • type −− 這是 s 允許儲存的元素型別。

返回值

方法呼叫返回指定集合的動態型別安全檢視。

異常

從整數集合獲取型別安全集合示例

以下示例演示瞭如何使用 Java Collection checkedSet(Set,Class ) 方法獲取整數集合的型別安全檢視。我們建立了一個包含一些整數的集合物件,並列印了原始集合。使用 checkedSet(Set, Integer) 方法,我們獲取了一個 Integer 集合,然後列印它。

package com.tutorialspoint;

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class CollectionsDemo {

   public static void main(String[] args) {
      Set<Integer> set = new TreeSet<>();

      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);

      System.out.println("Initial collection value: " + set);

      Set<Integer> safeSet = Collections.checkedSet(set, Integer.class);
      System.out.println("Typesafe View: "+safeSet);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Initial collection value: [1, 2, 3, 4, 5]
Typesafe View: [1, 2, 3, 4, 5]

從字串集合獲取型別安全集合示例

以下示例演示瞭如何使用 Java Collection checkedSet(Set,Class ) 方法獲取字串集合的型別安全檢視。我們建立了一個包含一些整數的集合物件,並列印了原始集合。使用 checkedSet(Set, String) 方法,我們獲取了一個 String 集合,然後列印它。

package com.tutorialspoint;

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class CollectionsDemo {

   public static void main(String[] args) {
      Set<String> set = new TreeSet<>();

      set.add("Welcome");
      set.add("to");
      set.add("Tutorialspoint");

      System.out.println("Initial collection value: " + set);

      Set<String> safeSet = Collections.checkedSet(set, String.class);
      System.out.println("Typesafe View: "+safeSet);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Initial collection value: [Tutorialspoint, Welcome, to]
Typesafe View: [Tutorialspoint, Welcome, to]

從物件集合獲取型別安全集合示例

以下示例演示瞭如何使用 Java Collection checkedSet(Set,Class ) 方法獲取 Student 物件集合的型別安全檢視。我們建立了一個包含一些 Student 物件的 Set 物件,並列印了原始集合。使用 checkedSet(Set, Student) 方法,我們獲取了一個 Students 集合,然後列印它。

package com.tutorialspoint;

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class CollectionsDemo {

   public static void main(String[] args) {
      Set<Student> set = new TreeSet<>();

      set.add(new Student(1, "Julie"));
      set.add(new Student(2, "Robert"));
      set.add(new Student(3, "Adam"));

      System.out.println("Initial collection value: " + set);

      Set<Student> safeSet = Collections.checkedSet(set, Student.class);
      System.out.println("Typesafe View: "+safeSet);
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }

   @Override
   public int compareTo(Student student) {
      return student.rollNo - this.rollNo;
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Initial collection value: [[ 3, Adam ], [ 2, Robert ], [ 1, Julie ]]
Typesafe View: [[ 3, Adam ], [ 2, Robert ], [ 1, Julie ]]
java_util_collections.htm
廣告