java Java中自定义类的Hashcode的实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14648175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementation of Hashcode for custom class in Java
提问by Dude
Possible Duplicate:
Overriding equals and hashCode in Java
I am supposed to implement hashcode and equals for Custom class Person. Person consists of
我应该为自定义类 Person 实现哈希码和等于。人包括
firstname
lastname
名
姓
I am supposed to implement equals and hashcode such that two people with firstnameand lastnameshould return true for equals and should be accepted by Hashmap. I have implemented Person class like this:
我应该实现equals和hashcode,这样两个名字和姓氏的人应该为equals返回true,并且应该被Hashmap接受。我已经实现了这样的 Person 类:
public class Person {
String firstname;
String lastname;
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return firstname.hashCode()+lastname.hashCode();
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
Person u=(Person) obj;
return u.firstname.equals(firstname) && u.lastname.equals(lastname);
}
}
Is the implementation of Hashcode correct here? Even though I am getting the expected result,I want to know if this implementation is correct?
这里 Hashcode 的实现是否正确?即使我得到了预期的结果,我也想知道这个实现是否正确?
采纳答案by MTilsted
There is a slight problem with your equals method because it will throw an exception if obj is null or not a Person
, so you should add the following to the top of your equals:
您的 equals 方法有一个小问题,因为如果 obj 为 null 或不是 a Person
,它将抛出异常,因此您应该将以下内容添加到您的 equals 的顶部:
if(obj==null || !(obj instanceof Person))
return false;
回答by mellamokb
There is an excellent discussion of proper equals
and hashCode
implementation here:
有正确的精彩讨论equals
和hashCode
实施在这里:
Whenever a.equals(b), then a.hashCode() must be same as b.hashCode()
每当 a.equals(b),那么 a.hashCode() 必须与 b.hashCode() 相同
This is the only rule that matters. There is no correctimplementation of a hashCode
besides this one rule. There are betterand worsehash codes in terms of performance and hash collisions, but that's another topic altogether.
这是唯一重要的规则。除了这个规则之外,没有正确的实现hashCode
。就性能和散列冲突而言,散列码有更好也更糟,但这完全是另一个话题。
Your code appears to be correct according to that rule, because if a.equals(b)
, then firstname.hashCode()+lastname.hashCode()
should be the same value for both a
and b
.
你的代码出现,根据这条规则是正确的,因为如果a.equals(b)
,那么firstname.hashCode()+lastname.hashCode()
应该是两个相同的数值a
和b
。
回答by posdef
hashCode()
is correct in the sense that it will work (assuming that the strings firstname and lastname are not null) - i.e. the method will return an int
. Whether it's a good solution or not is a much longer story, which I am sure you can check up on using the search field above ;)
hashCode()
从某种意义上说它是正确的(假设字符串 firstname 和 lastname 不为空) - 即该方法将返回一个int
. 它是否是一个好的解决方案是一个更长的故事,我相信您可以使用上面的搜索字段进行检查;)
Here's an interesting question I've asked a while back regarding custom implementations of hashCode()
: Using a larger prime as a multiplier when overriding hashCode()
这是一个有趣的问题,我曾经问过一个关于自定义实现的问题hashCode()
:在覆盖 hashCode() 时使用更大的素数作为乘数
回答by th3falc0n
Your code is fine. String has an good hash algorithm and just adding hashes is the most efficient way for hashing multiple Strings in Java.
你的代码没问题。String 有一个很好的散列算法,只是添加散列是在 Java 中散列多个字符串的最有效方法。