hashcode的实现_JAVA_编程开发_程序员俱乐部
hashcode的实现
- 摘要:ThinkinJava中给出了EffectiveJava的实现。给出int变量result赋予某个非零值常量,例如17.为对象内每个有意义的域f(即每个可以坐equals()操作的域)计算一个int的码c:boolean---c=(f?0:1)byte,char,short,int---c=(int)flong---c=(int)(f^(f>>>32))float---c=Float.floatToIntBits(f);double---longlDouble
- 标签:has 实现 Hash
- ThinkinJava中给出了Effective Java的实现。
- 给出int变量result赋予某个非零值常量,例如17.
- 为对象内每个有意义的域f(即每个可以坐equals()操作的域)计算一个int的码c:
- boolean --- c=(f?0:1)
- byte, char, short, int --- c=(int)f
- long --- c = (int) (f ^ (f >>> 32))
- float --- c = Float.floatToIntBits(f);
- double --- long l Double.doubleToLongBits(f); c = (int) (l ^ l >>>32))
- Object --- c=f.hashCode()
- Array --- every element do it.
- result = 37 * result + c;