重写HashCode和equals方法为什么要一起重写

1.equals与hashcode间的关系

    1.如果两个对象相同(即用equals比较返回true),那么它们的hashCode值一定要相同;

    2.如果两个对象的hashCode相同,它们并不一定相同(即用equals比较返回false)   

上面的解释明白吗?不明白我再通俗一点来讲

两个一样属性的对象,用equals比较是true,那么它们的hash值肯定是一样的。但是如果是hash值一样,不代表这两个对象就是一样的,因为hash计算的是在内存中的地址值,而不是对象本身的属性值。

重新两个方法其实是有规律的,对象中你要比哪一个,你就将属性加进去,我这里只是对比对象里面的id。所以你看到我重写后的方法是这样的。如果你的对象里面还有其他属性,也可以加上去就行。

    @Override
    public int hashCode() {
        return (id != null ? id.hashCode() : 1) * 31;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof BaseDto) {
            BaseDto o = (BaseDto) obj;
            return Objects.equals(id, o.getId());
        }
        return false;
    }

这边我写了一个测试类,可以对比一下在重写前和重写后的区别 SecondAuthSystemDto这个类是继承了我重写的BaseDto类的,效果是一样的


    public R<Map<Object,Object>> test01() {
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        SecondAuthSystemDto secondAuthSystemDto1 = new SecondAuthSystemDto();
        SecondAuthSystemDto secondAuthSystemDto2 = new SecondAuthSystemDto();

        boolean equals0 = secondAuthSystemDto1.equals(secondAuthSystemDto2);
        int i0 = secondAuthSystemDto1.hashCode();
        int i00 = secondAuthSystemDto2.hashCode();
        map.put("secondAuthSystemDto0--的哈希值",i0);
        map.put("secondAuthSystemDto00--的哈希值",i00);
        map.put("第0次比较secondAuthSystemDto1和secondAuthSystemDto2的结果",equals0);

        secondAuthSystemDto1.setId("adsaddas");
        secondAuthSystemDto2.setId("adsaddas");

        map.put(secondAuthSystemDto1,"存入第一个对象");
        map.put("能否取到值:",map.get(secondAuthSystemDto2));

        boolean equals1 = secondAuthSystemDto1.equals(secondAuthSystemDto2);
        map.put("第一次比较secondAuthSystemDto1和secondAuthSystemDto2的结果",equals1);

        int i1 = secondAuthSystemDto1.hashCode();
        int i2 = secondAuthSystemDto2.hashCode();
        map.put("secondAuthSystemDto1的哈希值",i1);
        map.put("secondAuthSystemDto2的哈希值",i2);

        secondAuthSystemDto1.setId("123456");
        secondAuthSystemDto2.setId("123456");
        boolean equals2 = secondAuthSystemDto1.equals(secondAuthSystemDto2);
        map.put("第二次比较secondAuthSystemDto1.setId(\"123456\");和secondAuthSystemDto2.setId(\"123456\");",equals2);

        int i3 = secondAuthSystemDto1.hashCode();
        int i4 = secondAuthSystemDto2.hashCode();
        map.put("第二次secondAuthSystemDto1的哈希值",i3);
        map.put("第二次secondAuthSystemDto2的哈希值",i4);

        secondAuthSystemDto1.setId("123456");
        secondAuthSystemDto2.setId("654321");
        boolean equals3 = secondAuthSystemDto1.equals(secondAuthSystemDto2);
        map.put("第三次比较secondAuthSystemDto1.setId(123456);和secondAuthSystemDto2.setId(654321);",equals3);

        int i5 = secondAuthSystemDto1.hashCode();
        int i6 = secondAuthSystemDto2.hashCode();
        map.put("第三次secondAuthSystemDto1的哈希值",i5);
        map.put("第三次secondAuthSystemDto2的哈希值",i6);

        return R.success(map);
    }

这里可以看到,重写前,就算你创建的对象的主键id是设置的一样的,但是输出的值是false,并且哈希值并不一样。

 当我们重写了hashCode和quals后,你可以看下面的结果,只要是相同的主键id,哈希值就是一样的,equals对比的结果也是true。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值