在hashCode方法的Best实现中接受的答案给出了一种看似很好的方法来查找哈希码.但我是Hash Codes的新手,所以我不知道该怎么做.
对于1),我选择的非零值是否重要?是1就像其他号码如黄金一样好31?
对于2),我是否将每个值添加到c?如果我有两个字段都是一个long,int,double,等?
我是否在本课程中正确解释了它:
public MyClass{
long a, b, c; // these are the only fields
//some code and methods
public int hashCode(){
return 37 * (37 * ((int) (a ^ (a >>> 32))) + (int) (b ^ (b >>> 32)))
+ (int) (c ^ (c >>> 32));
}
}
Run Code Online (Sandbox Code Playgroud)