当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Collections unmodifiableSortedMap()用法及代码示例


java.util.Collections类的unmodifiableSortedMap()方法用于返回指定的已排序映射的不可修改视图。此方法允许模块为用户提供对内部排序映射的只读访问权限。对返回的排序图“read through”到指定排序图的查询操作。尝试通过其集合视图或通过其subMap,headMap或tailMap视图直接修改返回的已排序Map会导致UnsupportedOperationException。

如果指定的排序图可序列化,则返回的排序图将可序列化。

用法:


public static <K, V> SortedMap<K, V>
    unmodifiableSortedMap(SortedMap<K, ? extends V> m)

参数:此方法将已排序的映射作为参数,要为其返回不可修改的视图。

返回值:此方法返回指定排序映射的不可修改视图。

以下示例说明了unmodifiableSortedMap()方法

示例1:

// Java program to demonstrate 
// unmodifiableSortedMap() method 
// for <String, String> value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        try { 
  
            // creating object of SortedMap<String, String> 
            SortedMap<String, String> 
                map = new TreeMap<String, String>(); 
  
            // populate the map 
            map.put("First", "10"); 
            map.put("Second", "20"); 
            map.put("Third", "30"); 
  
            // make the map unmodifiable 
            Map<String, String> 
                unmodsortmap = Collections 
                                   .unmodifiableSortedMap(map); 
  
            // printing unmodifiablemap 
            System.out.println("Initial sorted map value: "
                               + map); 
        } 
  
        catch (UnsupportedOperationException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
Initial sorted map value: {First=10, Second=20, Third=30}

示例2:对于UnsupportedOperationException

// Java program to demonstrate 
// unmodifiableSortedMap() method 
// For UnsupportedOperationException 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
  
        try { 
  
            // creating object of SortedMap<String, String> 
            SortedMap<String, String> 
                map = new TreeMap<String, String>(); 
  
            // populate the map 
            map.put("First", "10"); 
            map.put("Second", "20"); 
            map.put("Third", "30"); 
  
            // make the map unmodifiable 
            Map<String, String> 
                unmodsortmap = Collections 
                                   .unmodifiableSortedMap(map); 
  
            // printing unmodifiablemap 
            System.out.println("unmodifiableSortedMap value: "
                               + map); 
  
            System.out.println("\nTrying to modify"
                               + " the unmodifiable SortedMap"); 
            unmodsortmap.put("Forth", "40"); 
        } 
  
        catch (UnsupportedOperationException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
unmodifiableSortedMap value: {First=10, Second=20, Third=30}

Trying to modify the unmodifiable SortedMap
Exception thrown : java.lang.UnsupportedOperationException


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Collections unmodifiableSortedMap() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。