Java Collections checkedNavigableMap()

checkedNavigableMap() 用于获取指定的Navigable Map的动态类型安全视图。如果尝试插入Map的键或值类型错误,则将导致ClassCastException异常。

1 语法

public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K,V> m, Class<K> keyType, Class<V> valueType)

2 参数

m:为其返回动态类型安全视图的Map。

keyType:key的类型。

valueType:value的类型。

3 返回值

返回指定的Navigable Map的动态类型安全视图。

4 Collections checkedNavigableMap()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.checkedNavigableMap的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        NavigableMap<String, Integer> hmap = new TreeMap<>();
        //插入元素到Map
        hmap.put("A", 11);
        hmap.put("B", 12);
        hmap.put("C", 13);
        hmap.put("V", 14);
        //创建Map的类型安全视图
        System.out.println("Type safe view of the Navigable Map is: "+Collections.checkedNavigableMap(hmap,String.class,Integer.class));
    }
}

输出结果为:

Type safe view of the Navigable Map is: {A=11, B=12, C=13, V=14}

5 Collections checkedNavigableMap()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.checkedNavigableMap的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        NavigableMap<Integer, String> map = new TreeMap<>();
        map = Collections.checkedNavigableMap(map, Integer.class, String.class);
        map.put(1, "One");
        map.put(2, "Two");
        map.put(3, "Three");
        System.out.println("Navigable Map  Element: "+map);
        Map map2 = map;
        //map2.put("Four", 4);
        map2.put(4, 100);
        System.out.println("New Navigable Map  Element: "+map2);
    }
}

输出结果为:

Navigable Map  Element: {1=One, 2=Two, 3=Three}
Exception in thread "main" java.lang.ClassCastException: Attempt to insert class java.lang.Integer value into map with value type class java.lang.String
	at java.util.Collections$CheckedMap.typeCheck(Collections.java:3577)
	at java.util.Collections$CheckedMap.put(Collections.java:3620)
	at com.yiidian.Demo.main(Demo.java:22)

6 Collections checkedNavigableMap()示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.checkedNavigableMap的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        NavigableMap<String, Integer> hmap = new TreeMap<>();
        //Insert values in the Map
        hmap.put("A", 11);
        hmap.put("B", 12);
        hmap.put("C", 13);
        hmap.put("V", 14);
        System.out.println("Type safe view of the Navigable Map1 is: "+Collections.checkedNavigableMap(hmap,String.class,Integer.class));
        NavigableMap<Integer, String> hashmap = new TreeMap<>();
        hashmap.put(11, "A");
        hashmap.put(12, "B");
        hashmap.put(11, "A");
        hashmap.put(12, "B");
        //Create type safe view of the Map
        System.out.println("Type safe view of the Navigable Map2 is: "+Collections.checkedNavigableMap(hashmap,Integer.class,String.class));
    }
}

输出结果为:

Type safe view of the Navigable Map1 is: {A=11, B=12, C=13, V=14}
Type safe view of the Navigable Map2 is: {11=A, 12=B}

 

热门文章

优秀文章