Java名称冲突错误,尽管不同的方法签名 [英] Java name clash error, despite different method signatures
问题描述
我正在运行Java 8,以防万一。
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
公共类MultiMap< K,V>扩展AbstractMap< K,Collection>>>
{
私人地图< K,收藏< V>>地图;
public MultiMap()
{
super();
map = new HashMap<>();
}
// Fine
public void clear(K key)
{
get(key).clear();
$ b $ Fine
public boolean add(K key,V value)
{
if(!containsKey(key))$ b $ (键,新的ArrayList<>());
return get(key).add(value);
}
// KABOOM !!
//Name clash:MultiMap< K,V>类型的方法remove(K,V)与Map< K,V>类型的remove(Object,Object)具有相同的擦除但不覆盖它
public boolean remove(K key,V value)
{
if(!containsKey(key))
return false;
return get(key).remove(value);
}
@Override public Collection< V> put(K key,Collection< V>值)
{
return map.put(key,values);
}
@Override public Set< java.util.Map.Entry< K,Collection< V>>> entrySet()
{
return map.entrySet();
$ div $解析方案
但是在java.util.Map接口中没有定义这样的方法!
There 是 a Map 接口中使用rel =noreferrer> Map#remove(Object,Object)
方法;它是在Java 8中添加的。因此出现错误。
For fun, I'm trying to implement a "MultiMap" collection, like what already exists in the Apache Commons library. I'm getting an interesting error with my "remove(K key, V value)" method. The compiler says that there is a name clash - that it has the same erasure as "remove(Object, Object) of type Map". But there is no such method defined in the java.util.Map interface! Only a "remove(Object)" method - with one parameter, as opposed to my two parameter version. What's even more interesting is that if you manually remove the type information by replacing my "remove(K key, V value)" with "remove(Object key, Object value)", it compiles fine. Can anyone explain this phenomenon?
I'm running Java 8, in case that matters.
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MultiMap<K, V> extends AbstractMap<K, Collection<V>>
{
private Map<K, Collection<V>> map;
public MultiMap()
{
super();
map = new HashMap<>();
}
//Fine
public void clear(K key)
{
get(key).clear();
}
//Fine
public boolean add(K key, V value)
{
if(!containsKey(key))
put(key, new ArrayList<>());
return get(key).add(value);
}
//KABOOM!!
//"Name clash: The method remove(K, V) of type MultiMap<K,V> has the same erasure as remove(Object, Object) of type Map<K,V> but does not override it"
public boolean remove(K key, V value)
{
if(!containsKey(key))
return false;
return get(key).remove(value);
}
@Override public Collection<V> put(K key, Collection<V> values)
{
return map.put(key, values);
}
@Override public Set<java.util.Map.Entry<K, Collection<V>>> entrySet()
{
return map.entrySet();
}
}
But there is no such method defined in the java.util.Map interface!
There is a Map#remove(Object, Object)
method in the Map
interface; it was added in Java 8. Hence the error.
这篇关于Java名称冲突错误,尽管不同的方法签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!