Java Collections unmodifiableNavigableMap()

unmodifiableNavigableMap() 用于获取指定的NavigableMap的不可修改视图。

1 语法

public static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<? extends K, ? extends K> m)  

2 参数

m:要返回其不可修改视图的NavigableMap。

3 返回值

返回指定NavigableMap的不可修改视图。

4 unmodifiableNavigableMap()示例1

package com.yiidian;

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

public class Demo {

    public static void main(String[] args) {
        //Create object of hash map and Add values in the map
        NavigableMap<String,String> map = new TreeMap<String,String>();
        map.put("key1", "Java");
        map.put("key2", "Android");
        map.put("key3", "JavaScript");
        System.out.println("Initial Map: "+map);
        //Create unmodifiable navigable map
        NavigableMap<String,String> map2 = Collections.unmodifiableNavigableMap(map);
        System.out.println("Unmodifiable Navigable Map: " + map2);
        map.put("key4", "Yiidian");
        System.out.println("Unmodifiable Navigable Map after adding: " + map2);
    }
}

输出结果为:

Initial Map: {key1=Java, key2=Android, key3=JavaScript}
Unmodifiable Navigable Map: {key1=Java, key2=Android, key3=JavaScript}
Unmodifiable Navigable Map after adding: {key1=Java, key2=Android, key3=JavaScript, key4=Yiidian}

5 unmodifiableNavigableMap()示例2

package com.yiidian;

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

public class Demo {

    public static void main(String[] args) {
        NavigableMap<Integer, String> map = new TreeMap<>();
        map.put(1, "one");
        map.put(2, "two");
        System.out.println("Initial Unmodifiable Navigable Map: "+map);
        NavigableMap<Integer, String> map2 = Collections.unmodifiableNavigableMap(map);
        map2.put(3, "three");
    }
}

输出结果为:

Exception in thread "main" java.lang.UnsupportedOperationException
Initial Unmodifiable Navigable Map: {1=one, 2=two}
	at java.util.Collections$UnmodifiableMap.put(Collections.java:1457)
	at com.yiidian.Demo.main(Demo.java:19)

6 unmodifiableNavigableMap()示例3

package com.yiidian;

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

public class Demo {

    public static void main(String[] args) {
        NavigableMap<Integer, Integer> map = new TreeMap<>();
        map.put(1, 1001);
        map.put(2, 1002);
        map.put(3, 1003);
        map.put(4, 1004);
        map.put(5, 1005);
        System.out.println("Initial map: " + map);
        NavigableMap<Integer, Integer> map2 = Collections.unmodifiableNavigableMap(map);
        map.remove(4, 1004);
        System.out.print("Unmodifiable navigable map after remove(4, 1004): "+map2);
    }
}

输出结果为:

Initial map: {1=1001, 2=1002, 3=1003, 4=1004, 5=1005}
Unmodifiable navigable map after remove(4, 1004): {1=1001, 2=1002, 3=1003, 5=1005}

 

热门文章

优秀文章