Lombok的@Data生成的hashCode和equals方法坑
发布日期:2021-07-01 01:15:44
浏览次数:2
分类:技术文章
本文共 4903 字,大约阅读时间需要 16 分钟。
一、场景复现
创建两个lombok的@Data注解的类Pig实例,放进HashMap当key,map里面的数据居然被覆盖了。
package com.mk;import lombok.Data;@Datapublic class Pig extends Animal{ private String sex;}
package com.mk;import java.util.HashMap;import java.util.Map;public class TestLombok { public static void main( String[] args ) { Pig aPig = new Pig(); aPig.setName("A"); aPig.setColor("white"); aPig.setSex("male"); Pig bPig = new Pig(); bPig.setName("B"); bPig.setColor("black"); bPig.setSex("male"); Mapmap = new HashMap<>(); map.put(aPig, "1"); map.put(bPig, "2"); System.out.println("map.size():"+map.size()); System.out.println("map.get(aPig):"+map.get(aPig)); System.out.println("map.get(bPig):"+map.get(bPig)); System.out.println("map.keySet().iterator().next().getName():" + map.keySet().iterator().next().getName()); System.out.println("aPig.equals(bPig):"+aPig.equals(bPig)); }}
package com.mk;import lombok.Data;@Datapublic abstract class Animal { private String name; private String color;}
运行结果:
map.size():1map.get(aPig):2map.get(bPig):2map.keySet().iterator().next().getName():AaPig.equals(bPig):true
百思不得其解,明明没有重写hashcode和equals方法,却返回true
通过反编译生成的class,可以看得lombok帮类文件生成hashcode和equals方法。hashcode和equals方法使用类声明的所有属性方法生成的(不包含继承的父类属性方法)
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package com.mk;public abstract class Animal { private String name; private String color; public Animal() { } public String getName() { return this.name; } public String getColor() { return this.color; } public void setName(String name) { this.name = name; } public void setColor(String color) { this.color = color; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof Animal)) { return false; } else { Animal other = (Animal)o; if (!other.canEqual(this)) { return false; } else { Object this$name = this.getName(); Object other$name = other.getName(); if (this$name == null) { if (other$name != null) { return false; } } else if (!this$name.equals(other$name)) { return false; } Object this$color = this.getColor(); Object other$color = other.getColor(); if (this$color == null) { if (other$color != null) { return false; } } else if (!this$color.equals(other$color)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof Animal; } public int hashCode() { int PRIME = true; int result = 1; Object $name = this.getName(); int result = result * 59 + ($name == null ? 43 : $name.hashCode()); Object $color = this.getColor(); result = result * 59 + ($color == null ? 43 : $color.hashCode()); return result; } public String toString() { return "Animal(name=" + this.getName() + ", color=" + this.getColor() + ")"; }}
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package com.mk;public class Pig extends Animal { private String sex; public Pig() { } public String getSex() { return this.sex; } public void setSex(String sex) { this.sex = sex; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof Pig)) { return false; } else { Pig other = (Pig)o; if (!other.canEqual(this)) { return false; } else { Object this$sex = this.getSex(); Object other$sex = other.getSex(); if (this$sex == null) { if (other$sex != null) { return false; } } else if (!this$sex.equals(other$sex)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof Pig; } public int hashCode() { int PRIME = true; int result = 1; Object $sex = this.getSex(); int result = result * 59 + ($sex == null ? 43 : $sex.hashCode()); return result; } public String toString() { return "Pig(sex=" + this.getSex() + ")"; }}
二、解决方案
(1)不使用lombok对子类进行生成属性方法
(2)lombok生成属性方法的子类不能使用map、set等集合使用的hashcode和equals方法。
转载地址:https://maokun.blog.csdn.net/article/details/104738102 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月16日 06时53分20秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
wait3,wait4 用法
2019-05-02
服务器TIME_WAIT和CLOSE_WAIT详解和解决办法
2019-05-02
使用gprof来对程序的性能分析总结
2019-05-02
Python3中的Bytes和str之间的关系
2019-05-02
Python自动化运维 - day3 - 文件处理 - 函数part1
2019-05-02
Python自动化运维 - day9 - 进程与线程
2019-05-02
Python小程序之购物车
2019-05-02
Python自动化运维 - day14 - JavaScript基础
2019-05-02
Python自动化运维 - day12 - HTML基础
2019-05-02
Python 模拟SQL对文件进行增删改查
2019-05-02
Python自动化运维 - day15 - jQuery基础
2019-05-02
Python自动化运维 - day16 - bootstrap
2019-05-02
Python自动化运维 - day5 - 函数part3、模块与包
2019-05-02
Python自动化运维 - Django(一)Urls基础-Views基础-Orm基础
2019-05-02
Python学习笔记 - day3 - 数据类型及运算符
2019-05-02
Python自动化运维 - day6 - 常用模块
2019-05-02
史上最全的Maven Pom文件标签详解
2019-05-02
Python自动化运维 - Django(五)中间件 - 缓存 - 信号
2019-05-02
Python自动化运维 - day7 - 面向对象
2019-05-02
Python学习笔记 - day4 - 流程控制
2019-05-02