Java Collections checkedNavigableSet() 方法



描述

Java Collections checkedNavigableSet(NavigableSet<E>, Class<E>) 方法用於獲取指定可導航集的動態型別安全檢視。

宣告

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

public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s, Class<E> type)

引數

  • s − 此引數是將返回其動態型別安全檢視的可導航集。

  • type − 此引數是 s 允許儲存的元素型別。

返回值

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

異常

從整數集獲取型別安全可導航集示例

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

package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsDemo {

   public static void main(String[] args) {
      NavigableSet<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);

      NavigableSet<Integer> safeSet = Collections.checkedNavigableSet(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 checkedNavigableSet(NavigableSet,Class ) 方法的使用,以獲取字串集的型別安全檢視。我們建立了一個包含一些整數的集合物件,並列印了原始集合。使用 checkedNavigableSet(NavigableSet, String) 方法,我們獲取了一個 String 的可導航集,然後列印它。

package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsDemo {

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

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

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

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

輸出

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

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

從物件集獲取型別安全可導航集示例

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

package com.tutorialspoint;

import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class CollectionsDemo {

   public static void main(String[] args) {
      NavigableSet<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);

      NavigableSet<Student> safeSet = Collections.checkedNavigableSet(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
廣告

© . All rights reserved.