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


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


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

如果指定的Map可序列化,则返回的Map将可序列化

用法:


public static <K, V> Map<K, V> 
    unmodifiableMap(Map<? extends K, ? extends V> m)

参数:此方法将Map作为要返回其不可修改视图的参数。

返回值:此方法返回指定Map的不可修改视图。

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

示例1:

// Java program to demonstrate 
// unmodifiableMap() method 
// for <String, String> value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
  
        try { 
  
            // creating object of Hashtable<String, String> 
            Hashtable<String, String> 
                table = new Hashtable<String, String>(); 
  
            // populate the table 
            table.put("key1", "1"); 
            table.put("key2", "2"); 
            table.put("key3", "3"); 
  
            // getting unmodifiable map 
            // using unmodifiableMap() method 
            Map<String, String> m = Collections 
                                        .unmodifiableMap(table); 
  
            // printing the unmodifiableMap 
            System.out.println("Initial collection: " + table); 
        } 
  
        catch (UnsupportedOperationException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
Initial collection: {key3=3, key2=2, key1=1}

示例2:对于UnsupportedOperationException

// Java program to demonstrate 
// unmodifiableMap() method 
// for <String, String> value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
  
        try { 
  
            // creating object of Hashtable<String, String> 
            Hashtable<String, String> 
                table = new Hashtable<String, String>(); 
  
            // populate the table 
            table.put("key1", "1"); 
            table.put("key2", "2"); 
            table.put("key3", "3"); 
  
            // getting unmodifiable map 
            // using unmodifiableMap() method 
            Map<String, String> m = Collections 
                                        .unmodifiableMap(table); 
  
            // printing the unmodifiableMap 
            System.out.println("Initial collection: "
                               + table); 
  
            // Adding element to new Collection 
            System.out.println("\nTrying to modify"
                               + " the unmodifiablemap"); 
            m.put("key4", "4"); 
        } 
  
        catch (UnsupportedOperationException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
输出:
Initial collection: {key3=3, key2=2, key1=1}

Trying to modify the unmodifiablemap
Exception thrown : java.lang.UnsupportedOperationException


相关用法


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