【问题标题】:Java Generic types and equals values return by generic methods?Java泛型类型和等值由泛型方法返回?
【发布时间】:2015-10-08 00:27:10
【问题描述】:

我有以下代码:

public class SimpleHashMap<K, V> extends AbstractMap<K, V>{

static final int SIZE = 997;

@SuppressWarnings("unchecked")
LinkedList<MapEntry<K, V>>[] buckets =
        new LinkedList[SIZE];

public V put(K key, V value)
{
    V oldValue = null;
    int index = Math.abs(key.hashCode()) % SIZE;
    if(buckets[index] == null)
        buckets[index] = new LinkedList<MapEntry<K, V>>();
    LinkedList<MapEntry<K, V>> bucket = buckets[index];
    MapEntry<K, V> pair = new MapEntry<K, V>(key, value);
    boolean found = false;
    ListIterator<MapEntry<K, V>> iter = bucket.listIterator();
    int probes = 0;
    while(iter.hasNext())
    {
        MapEntry<K, V> iPair = iter.next();
        probes++;
            if(iPair.getKey().equals(key)){
                oldValue = iPair.getValue();
                iter.set(pair); //zastapienie starego nowym
                found = true;
                System.out.println("Colision at: " + iPair + " : " + probes + (probes == 1 ? " probe " : " probes ") 
                                        + "needed"); 
                break;
            }
    }

    if(!found)
        buckets[index].add(pair);
    return oldValue;
}

    public V remove(Object o)
{
    V removed = null;
    if(this.get(o) != null)
    {
        int index = Math.abs(o.hashCode()) % SIZE;
            for(MapEntry<K, V> pair : buckets[index])
            {
                if(pair.getKey().equals(o))
                {
                    removed = pair.getValue();
                    buckets[index].remove(buckets[index].indexOf(pair));
                    break;
                }
            }
    }
    return removed;
}

@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
    Set<Map.Entry<K, V>> set = new HashSet<Map.Entry<K, V>>();
    for(LinkedList<MapEntry<K, V>> bucket : buckets){
        if(bucket == null) continue;
        for(MapEntry<K, V> mpair : bucket)
            set.add(mpair);
    }
    return set;
   }
}

我对它的方法很感兴趣:

    public V remove(Object o)
{
    V removed = null;
    if(this.get(o) != null)
    {
        int index = Math.abs(o.hashCode()) % SIZE;
            for(MapEntry<K, V> pair : buckets[index])
            {
                if(pair.getKey().equals(o))
                {
                    removed = pair.getValue();
                    buckets[index].remove(buckets[index].indexOf(pair));
                    break;
                }
            }
    }
    return removed;
}

当我们以这种方式创建上述类的实例时:

SimpleHashMap<String, String> m = 
            new SimpleHashMap<String, String>();

add some elements by putAll method...;

为什么我做不到:

if(this.get(o).equals(o))

代替:

if(this.get(o) != null)

我知道在编译级别擦除类型,但如果上面的 .equals 意味着我们比较两个 Object 类型,所以我们比较的是地址,而不是值。好的,这是合乎逻辑的。 但我们在这里比较的是:

if(pair.getKey().equals(o))

看起来我们比较了两种字符串类型。现在我很困惑,有人可以向我解释这在这种情况下究竟是如何工作的吗?对不起我的英语不好。我希望有人能帮我解决这个问题。非常感谢。

【问题讨论】:

  • MapEntry 是自己的类型。它实现了 Map.Entry 接口。
  • 我不明白你的问题或与所有代码的相关性。
  • 感谢您的回复。我已经分析了代码,我明白了一切。我想知道的一切都与类型擦除有关。
  • 怎么样?这是一个广泛的主题。澄清你的问题。

标签: java generics compare equals


【解决方案1】:

remove(Object o) 的参数是一个key,所以if(pair.getKey().equals(o)) 将pair 的key 与key 参数进行比较(比较苹果和苹果),但是this.get(o) 返回一个值,所以if(this.get(o).equals(o)) 将值与键进行比较,这是没有意义的(将苹果与橙子进行比较)。

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    相关资源
    最近更新 更多