Java Collections emptyNavigableMap()

emptyNavigableMap() 返回一个不变的空NavigableMap。

1 语法

public static final <K,V> NavigableMap<K,V> emptyNavigableMap()  

2 参数

3 返回值

返回一个不变的空NavigableMap。

4 Collections emptyNavigableMap()示例1

package com.yiidian;

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

public class Demo {

    public static void main(String[] args) {
        //Create an empty Map
        NavigableMap<String,String> EmptyNavMap = Collections.emptyNavigableMap();
        System.out.println("Created Empty Navigable Map: "+EmptyNavMap);
    }
}

输出结果为:

Created Empty Navigable Map: {}

5 Collections emptyNavigableMap()示例2

package com.yiidian;

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

public class Demo {

    public static void main(String[] args) {
        //Create an empty Map
        NavigableMap<String,String> EmptyNavMap = Collections.emptyNavigableMap();
        System.out.println("Created Empty Navigable Map: "+EmptyNavMap);
        // try to add elements
        EmptyNavMap.put("1","from yiidian.com");
    }
}

输出结果为:

Created Empty Navigable Map: {}
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.Collections$UnmodifiableMap.put(Collections.java:1457)
	at com.yiidian.Demo.main(Demo.java:18)

6 Collections emptyNavigableMap()示例3

package com.yiidian;

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

public class Demo {

    public static void main(String[] args) {
        //Create an empty Map
        NavigableMap<Integer, Integer> empNavMap = Collections.emptyNavigableMap();
        empNavMap.put(1, 23424);
        empNavMap.put(2, 65776);
        System.out.println("Created empty immutable Navigable Map: "+empNavMap);
    }
}

输出结果为:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.Collections$UnmodifiableMap.put(Collections.java:1457)
	at com.yiidian.Demo.main(Demo.java:16)

 

热门文章

优秀文章