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


Java Collections unmodifiableCollection()用法及代碼示例


java.util.Collections類的unmodifiableCollection()方法用於返回指定集合的​​不可修改視圖。此方法允許模塊為用戶提供對內部集合的隻讀訪問權限。對返回的集合“read through”進行查詢操作到指定的集合,並嘗試直接或通過其迭代器修改返回的集合,將導致UnsupportedOperationException。

返回的集合不會將hashCode和equals操作傳遞到後備集合,而是依賴於Object的equals和hashCode方法。在後備集合是集合或列表的情況下,必須保留這些操作的合同。

如果指定的集合是可序列化的,則返回的集合將是可序列化的。


用法:

public static <T> Collection<T> 
    unmodifiableCollection(Collection<? extends T> c)

參數:此方法將集合作為要返回其不可修改視圖的參數。

返回值:此方法返回指定集合的​​不可修改視圖。

以下示例說明了unmodifiableCollection()方法

示例1:

// Java program to demonstrate 
// unmodifiableCollection() method 
// for <Character> Value 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // creating object of ArrayList<Character> 
            List<Character> list = new ArrayList<Character>(); 
  
            // populate the list 
            list.add('X'); 
            list.add('Y'); 
  
            // printing the list 
            System.out.println("Initial list: " + list); 
  
            // getting unmodifiable list 
            // using unmodifiableCollection() method 
            Collection<Character> 
                immutablelist = Collections 
                                    .unmodifiableCollection(list); 
        } 
        catch (UnsupportedOperationException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Initial list: [X, Y]

示例2:對於UnsupportedOperationException

// Java program to demonstrate 
// unmodifiableCollection() method 
// for UnsupportedOperationException 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
        try { 
  
            // creating object of ArrayList<Character> 
            List<Character> list = new ArrayList<Character>(); 
  
            // populate the list 
            list.add('X'); 
            list.add('Y'); 
  
            // printing the list 
            System.out.println("Initial list: " + list); 
  
            // getting unmodifiable list 
            // using unmodifiableCollection() method 
            Collection<Character> 
                immutablelist = Collections 
                                    .unmodifiableCollection(list); 
  
            // Adding element to new Collection 
            System.out.println("\nTrying to modify"
                               + " the unmodifiableCollection"); 
  
            immutablelist.add('Z'); 
        } 
  
        catch (UnsupportedOperationException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
Initial list: [X, Y]

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


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Collections unmodifiableCollection() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。