[英]add() method on entrySet() in Map<>
While iterating Map<>
using for loop 在使用for循环迭代Map<>
for(Map.Entry<K,V> mapEntry : myMap.entrySet()){
// something
}
I found entrySet()
method returns a set of Entry<K,V>
我发现entrySet()
方法返回一组Entry<K,V>
so it has add(Entry<K,V> e)
method 所以它有add(Entry<K,V> e)
方法
then I created a class which implements Map.Entry<K,V>
and tried inserting the object like below 然后我创建了一个实现Map.Entry<K,V>
的类,并尝试插入如下所示的对象
public final class MyEntry<K, V> implements Map.Entry<K, V> {
private final K key;
private V value;
public MyEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V old = this.value;
this.value = value;
return old;
}
}
Entry<String, String> entry = new MyEntry<String, String>("Hello", "hello");
myMap.entrySet().add(entry); //line 32
there is no compilation error, but it throws a runtime error 没有编译错误,但是会引发运行时错误
Exception in thread "main"
java.lang.UnsupportedOperationException
at java.util.AbstractCollection.add(AbstractCollection.java:262)
at com.seeth.AboutEntrySetThings.main(AboutEntrySetThings.java:32)
From the JavaDoc on entrySet()
method: 从entrySet()
方法上的JavaDoc:
The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. 该集合支持元素删除,该元素通过Iterator.remove,Set.remove,removeAll,retainAll和clear操作从映射中删除相应的映射。 It does not support the add or addAll operations. 它不支持add或addAll操作。
The javadoc for that method https://docs.oracle.com/javase/10/docs/api/java/util/AbstractCollection.html#add(E) says 该方法的Javadoc https://docs.oracle.com/javase/10/docs/api/java/util/AbstractCollection.html#add(E)说
This implementation always throws an UnsupportedOperationException. 此实现始终抛出UnsupportedOperationException。
Then subclasses override it and public void add(int size, E element);
然后,子类覆盖它并public void add(int size, E element);
The subclass EntrySet
in HashMap
does not override it. HashMap
中的子类EntrySet
不会覆盖它。
The problem is that you are calling add()
method on the entrySet()
of HashMap
and there is no such implementation in that class, only in its superclasses . 问题在于您正在HashMap
的entrySet()
上调用add()
方法,并且该类中没有此类实现,仅在其超类中存在 。
From HashMap
source code: 从HashMap
源代码:
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}
final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
// there is no add() override
}
Because add()
method is not overriden (neither in HashMap.EntrySet
, nor in AbstractSet
), a method from AbstractCollection
will be used, which has the following definition: 因为add()
均未在方法没有重写HashMap.EntrySet
,也不在AbstractSet
),从一个方法AbstractCollection
将被使用,其具有下面的定义:
public boolean add(E e) {
throw new UnsupportedOperationException();
}
Also, looking on the entrySet()
Javadoc : 另外,查看entrySet()
Javadoc :
(...) The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. (...)集合支持元素删除,该元素通过Iterator.remove,Set.remove,removeAll,retainAll和clear操作从映射中删除相应的映射。 It does not support the add or addAll operations. 它不支持add或addAll操作。
Method java.util.HashMap.entrySet() returns a class java.util.HashMap.EntrySet, which itself does not implement method Set.add(). 方法java.util.HashMap.entrySet()返回一个类java.util.HashMap.EntrySet,该类本身未实现方法Set.add()。
To add an object to the collection, you must use method myMap.put(entry.getKey(), entry.getValue()). 要将对象添加到集合中,必须使用方法myMap.put(entry.getKey(),entry.getValue())。
Method entrySet() is used only for reading data, and not for modification. 方法entrySet()仅用于读取数据,而不用于修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.