组合两个 Set 时的 java.lang.UnsupportedOperationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19950771/
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-12 21:47:10  来源:igfitidea点击:

java.lang.UnsupportedOperationException when combining two Sets

javacollectionsset

提问by user1236048

I have 2 different instances of HashMap

我有 2 个不同的 HashMap 实例

I want to merge the keysets of both HashMaps;

我想合并两个 HashMap 的键集;

Code:

代码:

Set<String> mySet = hashMap1.keySet();
mySet.addAll(hashMap2.keySet());

Exception:

例外:

java.lang.UnsupportedOperationException
    at java.util.AbstractCollection.add(AbstractCollection.java:238)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:322)

I don't get a compile warning or error.

我没有收到编译警告或错误。

From java doc this should work. Even if the added collection is also a set:

从 java doc 这应该可以工作。即使添加的集合也是一个集合:

boolean addAll(Collection c)

布尔添加所有(集合 c)

Adds all of the elements in the specified collection to this set if they're not already present (optional operation). If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

如果指定集合中的所有元素尚不存在,则将其添加到此集合中(可选操作)。如果指定的集合也是一个集合,addAll 操作会有效地修改这个集合,使其值是两个集合的并集。如果在操作进行时修改了指定的集合,则此操作的行为未定义。

采纳答案by SudoRahul

If you look at the docs of the HashMap#keySet()method, you'll get your answer(emphasis mine).

如果您查看该HashMap#keySet()方法的文档,您将得到答案(重点是我的)。

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

返回此映射中包含的键的 Set 视图。该集合由地图支持,因此对地图的更改会反映在该集合中,反之亦然。如果在对集合进行迭代时修改了映射(通过迭代器自己的删除操作除外),则迭代的结果是不确定的。该集合支持元素移除,通过 Iterator.remove、Set.remove、removeAll、retainAll 和 clear 操作从映射中移除相应的映射。它不支持 add 或 addAll 操作。

Therefore, you need to create a new set and add all the elements to it, instead of adding the elements to the Setreturned by the keySet().

因此,你需要创建一个新的集合,所有的元素添加的,而不是添加元素的它,Set被返回的keySet()

回答by NPE

The result of keySet()does not support adding elements to it.

的结果keySet()不支持向其添加元素。

If you are not trying to modify hashMap1but just want a set containing the union of the two maps' keys, try:

如果您hashMap1不想修改而只是想要一个包含两个映射键的并集的集合,请尝试:

Set<String> mySet = new HashSet<String>();
mySet.addAll(hashMap1.keySet());
mySet.addAll(hashMap2.keySet());

回答by VinayVeluri

Doesn't support by nature of Set which is from map.keySet(). It supports only remove, removeAll, retainAll, and clear operations.

不支持来自 map.keySet() 的 Set 的性质。它只支持remove、removeAll、retainAll 和clear 操作。

Please read documentation

请阅读文档

回答by nantitv

All the above answers are correct. If you still wants to know the exact implementation detail (jdk 8)

以上所有答案都是正确的。如果您仍然想知道确切的实现细节(jdk 8)

hashMap1.keySet() returns a KeySet<E>and

hashMap1.keySet() returns a KeySet<E>

KeySet<E>   extends AbstractSet<E>
AbstractSet<E> extends AbstractCollection<E> 

In AbstractCollection,

在抽象集合中,

public boolean add(E e) {
        throw new UnsupportedOperationException();
    }

addAll() calls add()and thats why you are getting an UOException

addAll() calls add()这就是为什么你得到一个 UOException