The entrySet() method returns Set<Map.Entry<K,V>> in HashMap/HashTable. Why the set does not support add/addAll operations, we know key and value entry?
I noticed the java.util.Hashtable.EntrySet.add(Map.Entry<K, V> o) implementation as follow in Jdk1.8:
private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public boolean add(Map.Entry<K,V> o) {
// MyNote: Call AbstractCollection<E>.add(E e) and
// throw UnsupportedOperationException
return super.add(o);
}
}
Why does not implement to support add operation like follow:
private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
/**
* @return <tt>false</tt> if key has exists
*/
public boolean add(Map.Entry<K,V> o) {
V old = Hashtable.this.put(o.getKey(), o.getValue());
return (null == old);
}
}