Lombok的@Data生成的hashCode和equals方法坑
方法 生成 Data equals Lombok hashCode
2023-09-14 09:06:16 时间
一、场景复现
创建两个lombok的@Data注解的类Pig实例,放进HashMap当key,map里面的数据居然被覆盖了。
package com.mk;
import lombok.Data;
@Data
public 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");
Map<Pig, String> map = 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));
}
}
运行结果:
map.size():1
map.get(aPig):2
map.get(bPig):2
map.keySet().iterator().next().getName():A
aPig.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方法。
相关文章
- rails自动生成大量记录的方法
- 织梦生成时提示DedeTag Engine Create File False的解决方法
- PHP 获取视频时长方法
- iOS App设置icon,启动图,App名称的方法
- Jquery.ajax报parseerror Invalid JSON错误的原因和解决方法:不能解析
- IDEA 运行junit单元测试方法
- 工厂方法模式
- 【问题解决方案】在Markdown中生成可以跳转到正文的目录的方法
- webpack打包生成多个vendor的配置方法
- Atitit 学习的方法 attilax总结
- Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子
- Python编程语言学习:包导入和模块搜索路径(包路径)简介、使用方法(python系统环境路径的查询与添加)之详细攻略
- Paper:《Graph Neural Networks: A Review of Methods and Applications—图神经网络:方法与应用综述》翻译与解读
- 推理网络精度不达标,5个方法轻松搞定
- 解决javac注解处理器生成的方法,在编译时报错“找不到符号”
- 【Android Gradle 插件】ProductFlavor 配置 ( ProductFlavor#buildConfigField 方法 | 单独编译生成 BuildConfig 类的任务 )
- Python极简方法生成Zip文件
- [C#] 解决使用ClosedXML生成Excel报表丢失样式的两种方法
- String类的常用成员方法
- 用QRCode.js制作二维码的生成器(qrcode.makeCode方法生成二维码)