首页 > 其他 > 详细

计算hashCode通用计算公式

时间:2018-02-26 10:32:03      阅读:214      评论:0      收藏:0      [点我收藏+]

1

@Override
    public int hashCode() {
        //设置初始值
        int result = 17;

        //假设有效域为: name,age,idCardNo,incomeAnnual,sex,brithDay
        int c = 0;
        //计算name (String为对象类型,他的计算直接调用本身的hashCode)
        c = name.hashCode();
        result = result * 37 + c;

        //计算age (int/byte/char/short类型,他的计算直接调用本身的值)
        c = this.getAge();
        result = result * 37 + c;

        //计算idCardNo (long类型,他的计算 (int)(field^(field >>> 32)) 无符号右移32位)
        c = (int) (this.idCardNo ^ (this.idCardNo >>> 32));
        result = result * 37 + c;

        //计算 incomeAnnual (double类型,他的计算 Double.doubleToLongBits(field)后,再按Long类型计算 )
        //(float类型,他的计算 Float.floatToIntBits(field)  )
        long tmp = Double.doubleToLongBits(this.incomeAnnual);
        c = (int) (tmp ^ (tmp >>> 32));
        result = result * 37 + c;

        //计算 sex (sex为boolean类型,他的计算直接调用 c=sex?1:0)
        c = this.isSex() ? 1 : 0;
        result = result * 37 + c;

        //计算 brithDay (brithDay为Date对象类型,他的计算直接调用 本身的hashCode)
        c = this.getBirthDay().hashCode();
        result = result * 37 + c;

        return result;
    }

 

计算hashCode通用计算公式

原文:https://www.cnblogs.com/zhshlimi/p/8471752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!