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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。