问个hashtable实现问题# Java - 爪哇娇娃
f*i
1 楼
我自己写了一个类
static class Mix{
public int a; double b; String c;
Mix(int a,double d,String c){
this.a=a;this.b=d;this.c=c;
}
public int hashCode(){
return 1;
}
}
在main函数中,我创建了两个一样的实例,但是hashtable认为他们是不一致的,我已
经重载了这个类的hashCode这个函数了啊。应该如何处理呢?
public static void main(String[] args)
Mix m1 = new Mix(1,0.53,"yes");
Mix m2 = new Mix(1,0.52,"yes");
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());
Hashtablehash = new Hashtable();
hash.put(m1, m1.hashCode());
if(hash.get(m2)!=null)
System.out.println("found");
else
System.out.println("not found");
System.out.println(hash.get(m1));
}
输出
1
1
not found
1
如何才能让hashtable认为它们一致呢,是我写错了覆盖函数吗?
static class Mix{
public int a; double b; String c;
Mix(int a,double d,String c){
this.a=a;this.b=d;this.c=c;
}
public int hashCode(){
return 1;
}
}
在main函数中,我创建了两个一样的实例,但是hashtable认为他们是不一致的,我已
经重载了这个类的hashCode这个函数了啊。应该如何处理呢?
public static void main(String[] args)
Mix m1 = new Mix(1,0.53,"yes");
Mix m2 = new Mix(1,0.52,"yes");
System.out.println(m1.hashCode());
System.out.println(m2.hashCode());
Hashtable
hash.put(m1, m1.hashCode());
if(hash.get(m2)!=null)
System.out.println("found");
else
System.out.println("not found");
System.out.println(hash.get(m1));
}
输出
1
1
not found
1
如何才能让hashtable认为它们一致呢,是我写错了覆盖函数吗?