Java Collections  checkedNavigableSet()
 
 
 
  checkedNavigableSet()是Java Collections 类的内置方法。此方法用于获取指定的导航集的动态类型安全视图。如果尝试插入值类型错误的元素,则将通过立即ClassCastException进行插入。 
 
语法
 
 以下是
  checkedNavigableSet()方法的声明: 
 
 
  
  
public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s, Class<E> type) 
 
   
  
参数
 
 
 
   
   | 参数 |  
   说明 |  
   必需/可选 |  
  
 
   
   |  s  |  
   这是要为其返回动态类型安全视图的导航集。 |  
   必需 |  
  
 
   
   | type |  
   这是允许s保留的元素的类型。 |  
   必需 |  
  
 
 
 
返回
 
 
  checkedNavigableSet()方法返回指定导航集的动态类型安全视图。
 
 异常 
 
   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)