Java 集合上的 UnsupportedOperationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2887590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:03:55  来源:igfitidea点击:

UnsupportedOperationException on Collection

javaexceptioncollections

提问by Duke Vador

While studying the CollectionAPI, we find that some methods (add, remove,...) may throw a java.lang.UnsupportedOperationExceptionif the current implementation of the Collection does not support those functionalities.

在研究CollectionAPI 时,我们发现如果 Collection 的当前实现不支持这些功能,则某些方法 ( add, remove,...) 可能会抛出 a java.lang.UnsupportedOperationException

Is there,actually, in the JDK, a concrete Collectionthat does not support those methods ?

实际上,在 JDK 中是否有Collection不支持这些方法的具体方法?

Thanks a lot for your answers.

非常感谢你的回答。

采纳答案by Sean Owen

The obvious examples are the implementations returned from, say, Collections.unmodifiableCollection()and other similar methods. Methods that would change the Collectionthrow this exception.

明显的例子是从Collections.unmodifiableCollection()其他类似方法返回的实现。将更改Collection抛出此异常的方法。

回答by Bozho

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 的私有类,因此您无法实例化它们。

回答by Péter T?r?k

Apart from the collections returned by the Collections.unmodifiable*methods, there are a couple more of interesting cases where UnsupportedOperationExceptionis actually thrown:

除了Collections.unmodifiable*方法返回的集合之外,还有一些有趣的情况UnsupportedOperationException实际上是抛出的:

  • the collection views of a Map, accessed via entrySet(), keySet()and values()can have elements removed but not added,
  • the list view returned by Arrays.asListcan have elements neither added nor removed,
  • 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.
  • 收集的意见的Map,通过访问entrySet()keySet()并且values()可以具有元件移除,但不添加,
  • 返回的列表视图Arrays.asList既不能添加也不能删除元素,
  • 此外,从Collections.empty*Collections.singleton*方法获得的对象也被标记为"immutable",所以 - 虽然它没有在 API 文档中明确说明 - 我想这些在尝试修改它们时也会抛出异常。

回答by Ruthreshwar

Normally when you create a list like List<String> sample=Collections.emptyList();. The List samplewill be created as a Collections.unmodifiableCollection().

通常当你创建一个像List<String> sample=Collections.emptyList();. 该列表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>();. In this list you can perform sample.add(), sample.addAll()etc...

  • 所以列表示例不支持动态列表操作。您只能使用赋值运算符将另一个列表分配给此列表。例如>

    List<String> ls=new ArrayList<String>();
    ls.add("one");
    ls.add("Three");
    ls.add("two");
    ls.add("four"); 
    sample = ls;
    
  • 对于动态列表操作,您应该使用类似 List<String> sample= new ArrayList<String>();. 在此列表中,您可以执行sample.add(), sample.addAll()等...