简体   繁体   English

Collection上的UnsupportedOperationException

[英]UnsupportedOperationException on Collection

While studying the Collection API, we find that some methods ( add , remove ,...) may throw a java.lang.UnsupportedOperationException if the current implementation of the Collection does not support those functionalities. 在研究Collection API时,我们发现如果Collection的当前实现不支持这些功能,某些方法( addremove ,...)可能会抛出java.lang.UnsupportedOperationException

Is there,actually, in the JDK, a concrete Collection that does not support those methods ? 实际上,JDK中是否有一个不支持这些方法的具体Collection

Thanks a lot for your answers. 非常感谢你的回答。

Apart from the collections returned by the Collections.unmodifiable* methods, there are a couple more of interesting cases where UnsupportedOperationException is actually thrown: 除了Collections.unmodifiable*方法返回的集合之外,还有一些有趣的案例,其中实际抛出了UnsupportedOperationException

  • the collection views of a Map , accessed via entrySet() , keySet() and values() can have elements removed but not added, 通过entrySet()keySet()values()访问的Map的集合视图可以删除元素但不添加元素,
  • the list view returned by Arrays.asList can have elements neither added nor removed, Arrays.asList返回的列表视图Arrays.asList可以添加也不删除元素,
  • moreover, the objects obtained from the Collections.empty* and Collections.singleton* methods are also marked as "immutable" , so - although it is not explicitly stated in the API docs - I suppose these throw the exception as well on attempts to modify them. 此外,从Collections.empty*Collections.singleton*方法获得的对象也被标记为“不可变” ,所以 - 虽然它没有在API文档中明确说明 - 我想这些也会在尝试修改时抛出异常他们。

The obvious examples are the implementations returned from, say, Collections.unmodifiableCollection() and other similar methods. 显而易见的例子是从Collections.unmodifiableCollection()和其他类似方法返回的实现。 Methods that would change the Collection throw this exception. 更改Collection方法会抛出此异常。

Normally when you create a list like List<String> sample=Collections.emptyList(); 通常,当您创建List<String> sample=Collections.emptyList(); . The List sample will be created as a Collections.unmodifiableCollection() . List sample将创建为Collections.unmodifiableCollection()

  • So the list sample does not support dynamic list operations. 因此列表示例不支持动态列表操作。 You can only assign another list to this list using assignment operator. 您只能使用赋值运算符将另一个列表分配给此列表。 Eg> 例如>

     List<String> ls=new ArrayList<String>(); ls.add("one"); ls.add("Three"); ls.add("two"); ls.add("four"); sample = ls; 
  • For dynamic list operations you should have a syntax like List<String> sample= new ArrayList<String>(); 对于动态列表操作,您应该具有类似List<String> sample= new ArrayList<String>();的语法List<String> sample= new ArrayList<String>(); . In this list you can perform sample.add(), sample.addAll() etc... 在此列表中,您可以执行sample.add(), sample.addAll()等...

Yes. 是。 For example when you call Collections.unmodifiableList(list) , the returned list does not support add(..) 例如,当您调用Collections.unmodifiableList(list) ,返回的列表不支持add(..)

These collections, however, are mostly private classes which are not exposed an an API, so you cannot instantiate them. 但是,这些集合大多是私有类,不会公开API,因此您无法实例化它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM