} DataForm 的equals 和hashcode算法都是采用7楼的算法 执行结果是 form hashCode: 113900820,anotherForm hashCode: 115040531,otherForm hashCode: 111883543 form equals otherForm = true,anotherForm equals otherForm = true,anotherForm equals form = true 相同的对象hashCoding值不相同啊!
equals 错了吧,应该是名字和年龄都相同才equals吧name.equals(form.getName()) && age == form.getAge();
我知道错在那里了,equals 最后一句是从你原来的那里复制过来的。 把 || 改为 &&
恩 如果改成是 name.equals(form.getName()) && age == form.getAge(); 的话就没有问题了。 不过如果我就name和age如果有一个相等及认为这两个对象相等。那么hashCode方法是不是就没发写了啊。 不过非常感谢各位大侠的帮助。
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Test other = (Test) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }
1, 如果a.equals(b),那么a.hashCode() == b.hashCode()。
2, 对象创建之后,hashCode()不能因为成员变量的改变而改变
3, 好像还有什么来着……
equals上加:
@OverridehashCode:
return name.hashCode - age;
hashCode:
return name.hashCode + age;
return name.hashCode()+age;
}
if(other == null) return false;
if(this == other) return true;
if(getClass() != other.getClass()) return false;
DataForm form = (DataForm)other;
return name.equals(form.getName()) || age == form.getAge();
}public int hashCode(){
return name.hashCode() * 37 + age;
}
上面重写equals方法写的好!
不过下面hashCoding 为什么要乘以37不太明白,还请大虾指教!
你可以使用其他质数,37不过是Effective Java 这样写。
DataForm form = new DataForm();
form.setAge(20);
form.setName("dddd");
DataForm anotherForm = new DataForm();
anotherForm.setAge(20);
anotherForm.setName("eeex");
DataForm otherForm = new DataForm();
otherForm.setAge(20);
System.out.println("form equla fom = " + form.hashCode() +","+ anotherForm.hashCode() +","+ otherForm.hashCode());
System.out.println("form equla fom = " + form.equals(otherForm) + anotherForm.equals(otherForm) + anotherForm.equals(form));
}
DataForm 的equals 和hashcode算法都是采用7楼的算法
执行结果是
form hashCode: 113900820,anotherForm hashCode: 115040531,otherForm hashCode: 111883543
form equals otherForm = true,anotherForm equals otherForm = true,anotherForm equals form = true
相同的对象hashCoding值不相同啊!
把 || 改为 &&
如果改成是 name.equals(form.getName()) && age == form.getAge(); 的话就没有问题了。
不过如果我就name和age如果有一个相等及认为这两个对象相等。那么hashCode方法是不是就没发写了啊。
不过非常感谢各位大侠的帮助。
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Test other = (Test) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
如果名称或年龄一样就 equals 为true
那么hashCode需要反映这个情况参考实现
1.name.hashCode() + age;
2.name.hashCode() ^ age;具体看另外一帖的讨论
http://topic.csdn.net/u/20100114/15/a3d20913-ac99-477f-af58-a6fb40856fb6.html?52794
修正下
用 name.hashCode() | age 或许更合适一些