计算hashCode通用计算公式

1.java计算公式

@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;
    }

 2. .net计算公式

public class HashCodeTest
    {
        public static void Excute()
        {
            var man = new Man()
            {
                Age = 10,
                BirthDay = new DateTime(1950,1,1),
                IdCardNo = 2139095040,
                IncomeAnnual = 10000000.5,
                Name = "Aven",
                Sex = true
            };
            var hasCode = man.GetHashCode();
            Console.WriteLine(hasCode);
        }
    }

    class Man
    {
        public long IdCardNo { get; set; }
        public int Age { get; set; }
        public string Name { get; set; }
        public double IncomeAnnual { get; set; }
        public bool Sex { get; set; }
        public DateTime BirthDay { get; set; }

        public override int GetHashCode()
        {
            //设置初始值
            int result = 17;

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

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

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

            //计算 incomeAnnual (double类型,他的计算 BitConverter.DoubleToInt64Bits(field)后,再按Long类型计算 )
            //(float类型,他的计算 BitConverter.ToInt32(BitConverter.GetBytes(this.IncomeAnnual),0) )
            long tmp = BitConverter.DoubleToInt64Bits(this.IncomeAnnual);
            c = (int)(tmp ^ (tmp >> 32));
            result = result * 37 + c;

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

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

            return result;
        }
    }

 

转载于:https://www.cnblogs.com/zhshlimi/p/8471752.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值