日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Lombok的@Data生成的hashCode和equals方法坑

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lombok的@Data生成的hashCode和equals方法坑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、場景復現

創建兩個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));} } package com.mk; import lombok.Data; @Data public abstract class Animal {private String name;private String color; }

運行結果:

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方法。

總結

以上是生活随笔為你收集整理的Lombok的@Data生成的hashCode和equals方法坑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。