当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。