Java名称冲突错误,尽管不同的方法签名 [英] Java name clash error, despite different method signatures

查看:142
本文介绍了Java名称冲突错误,尽管不同的方法签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了好玩,我试图实现一个MultiMap集合,就像Apache Commons库中已经存在的那样。我在删除(K键,V值)方法中遇到了一个有趣的错误。编译器说有一个名称冲突 - 它与删除(Object,Object)类型为Map的擦除相同。但是在java.util.Map接口中没有定义这样的方法!只有一个remove(Object)方法 - 带有一个参数,而不是我的两个参数版本。更有意思的是,如果通过用remove(Object key,Object value)替换我的remove(K key,V value)来手动删除类型信息,它会很好地编译。任何人都可以解释这种现象吗?



我正在运行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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆