Integer没有复写hashCode么?晕哦,我也不是很记得拉。
第二个你创建了两个Tc对象,两个不同的KEY值,所以存入俩

解决方案 »

  1.   

    还需要重载equals方法
    public class Tc {
    public void equals(Object obj) {
    return hashCode() == obj.hashCode();
    }
    }
      

  2.   

    看HashMap的source code
        public boolean containsKey(Object key) {
            Object k = maskNull(key);
            int hash = hash(k);
            int i = indexFor(hash, table.length);
            Entry e = table[i]; 
            while (e != null) {
                if (e.hash == hash && eq(k, e.key)) 
                    return true;
                e = e.next;
            }
            return false;
        }可以看到在比较的时候除了hashCode之外还要调用eq方法
    而eq方法中调用了equals方法
        static boolean eq(Object x, Object y) {
            return x == y || x.equals(y);
        }