【发布时间】:2014-07-10 12:31:46
【问题描述】:
我正在尝试覆盖 PutAll 以循环遍历这些对并在它们上调用 put,因为我使用的是自定义 HashMap 对象。我试过这个:
@Override
public void putAll(final Map m) {
Iterator iterator = m.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pairs = (Map.Entry)iterator.next();
put(pairs.getKey(), pairs.getValue());
}
}
但它给了我以下错误:
Compiling the source code....
$javac HelloWorld.java 2>&1
HelloWorld.java:109: error: no suitable method found for put(Object,Object)
put(pairs.getKey(), pairs.getValue());
^
method UniqueHashMap.put(K,V) is not applicable
(actual argument Object cannot be converted to K by method invocation conversion)
method HashMap.put(K,V) is not applicable
(actual argument Object cannot be converted to K by method invocation conversion)
method AbstractMap.put(K,V) is not applicable
(actual argument Object cannot be converted to K by method invocation conversion)
where K,V are type-variables:
K extends Object declared in class UniqueHashMap
V extends Object declared in class UniqueHashMap
1 error
【问题讨论】:
标签: java hashmap overriding key-value