7

我在搞乱一些 hashCode + equals + Map 的东西,发现了一些……奇怪的东西。

片段如下:

class Obj {
    String n;
    Obj(String n) {this.n = n;}

    public int hashCode() {return 0;}
    public boolean equals(Object o) {return false;} // no instance of this class 
                                                    // equals any other instance

}

然后我做了这样的事情:

    java.util.Map<Obj,String> map = new java.util.HashMap<Obj,String>();
    Obj o1 = new Obj("1");
    Obj o11 = new Obj("1");
    Obj o2 = new Obj("2");

    map.put(o1,"val 1");
    map.put(o11,"val 2");
    map.put(o2,"val 3");

    p("size = " + map.size()); // obviously 3
    p(map.get(new Obj("1"))); // obviously null     
    p(map.get(o1)); // ...

最后一行是奇怪的部分。最后一行返回val 1。怎么来的?该equals方法总是返回false。这是因为在调用==之前使用了运算符吗?equals

感谢您的任何见解。

4

2 回答 2

8

HashMap.java中,get方法是:

public V get(Object key) {
    if (key == null)
        return getForNullKey();
    int hash = hash(key.hashCode());
    for (Entry<K,V> e = table[indexFor(hash, table.length)];
         e != null;
         e = e.next) {
        Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
            return e.value;
    }
    return null;
}

该行if (e.hash == hash && ((k = e.key) == key || key.equals(k)))确实比较了==调用之前使用的键equals。这就是为什么你试图消除平等失败的原因。

于 2012-11-22T23:18:33.583 回答
2

哈希映射实现检查 == 和 equals 键的相等性。(如果 hashCode 相同)

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/HashMap.java#HashMap.get%28java.lang.Object%29

于 2012-11-22T23:18:13.443 回答