當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Collections checkedNavigableSet()用法及代碼示例

checkedNavigableSet() 是 Java Collections 類的內置方法。此方法用於獲取指定 Navigable Set 的動態類型安全視圖。如果任何試圖插入其值具有錯誤類型的元素的嘗試,它將立即通過 ClassCastException。

用法

以下是 checkedNavigableSet() 方法的聲明:

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

參數

參數 描述 必需/可選
s 它是將為其返回動態類型安全視圖的可導航集。 Required
type 它是 s 允許持有的元素類型。 Required

返回

checkedNavigableSet() 方法返回指定 Navigable Set 的動態類型安全視圖。

異常

ClassCastException

兼容版本

Java 1.8 及以上

例子1

import java.util.*;
public class CollectionsCheckedNavigableSetExample1 {
    public static void main(String[] args) {
          NavigableSet<String> set = new TreeSet<>();
          //Insert values in the Map
          set.add("A1");
          set.add("B2");
          set.add("C3");
          set.add("D4");
            //Create type safe view of the Set        
            System.out.println("Type safe view of the Navigable Set is:"+Collections.checkedNavigableSet(set,String.class));
            }
}

輸出:

Type safe view of the Navigable Set is:[A1, B2, C3, D4]

例子2

import java.util.*;
public class CollectionsCheckedNavigableSetExample2 {
    public static void main(String[] args) {
        NavigableSet<Integer> set = new TreeSet<>();
        //Insert values in the Map
        set.add(11);
        set.add(22);
        set.add(33);
        set.add(44);
          //Create type safe view of the Set  
        NavigableSet<Integer> TypeSafeSet;
        TypeSafeSet = Collections.checkedNavigableSet(set,Integer.class);
          System.out.println("The view of the Navigable Set is:"+TypeSafeSet);
          }
}

輸出:

The view of the Navigable Set is:[11, 22, 33, 44]

例子3

import java.util.*;
public class CollectionsCheckedNavigableSetExample3 {
    public static void main(String[] args) {
        NavigableSet<String> set = new TreeSet<>();
        //Insert values in the Map
        set.add("A");
        set.add("B");
        set.add("A");
        set.add("B");
        System.out.println("Type safe view of the Navigable Set1 is:"+Collections.checkedNavigableSet(set,String.class));
        NavigableSet<Integer> sset = new TreeSet<>();
        sset.add(11);
        sset.add(12);
        sset.add(11);
        sset.add(12);
        //Create type safe view of the Map        
        System.out.println("Type safe view of the Navigable Set2 is:"+Collections.checkedNavigableSet(sset,Integer.class)); 
        Set sset2 = sset;
          sset2.add("two");
          System.out.println("TypeSafe View:"+sset2);
        }
}

輸出:

Type safe view of the Navigable Set1 is:[A, B]
Type safe view of the Navigable Set2 is:[11, 12]
Exception in thread "main" java.lang.ClassCastException:java.base/java.lang.Integer cannot be cast to java.base/java.lang.String
	at java.base/java.lang.String.compareTo(String.java:124)
	at java.base/java.util.TreeMap.put(TreeMap.java:566)
	at java.base/java.util.TreeSet.add(TreeSet.java:255)
	at myPackage.CollectionCheckedNavigableSetExample3.main(CollectionCheckedNavigableSetExample3.java:21)





相關用法


注:本文由純淨天空篩選整理自 Java Collections checkedNavigableSet() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。