Java Collections emptySortedMap()

emptySortedMap() 返回一个不变的空SortedMap。

1 语法

public static final <K,V> SortedMap<K,V> emptySortedMap()  

2 参数

3 返回值

返回一个不变的空SortedMap。

4 Collections emptySortedMap()示例1

package com.yiidian;

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

public class Demo {

    public static void main(String[] args) {
        SortedMap<String,String> EmpSortedMap = Collections.emptySortedMap();
        System.out.println("Created Empty Sorted Map: "+EmpSortedMap);
    }
}

输出结果为:

Created Empty Sorted Map: {}

5 Collections emptySortedMap()示例2

package com.yiidian;

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

public class Demo {

    public static void main(String[] args) {
        SortedMap<String,String> EmpSortedMap = Collections.emptySortedMap();
        System.out.println("Created Empty Sorted Map: "+EmpSortedMap);
        // try to add elements
        EmpSortedMap.put("1","from JavaTpoint.com");
    }
}

输出结果为:

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

6 Collections emptySortedMap()示例3

package com.yiidian;

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

public class Demo {

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

输出结果为:

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

 

热门文章

优秀文章