當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。