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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322665.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vscode多行选中

shiftalt移动光标&#xff0c;可以连续同时编辑多行内容

jzoj4671-World Tour【图论,bfs】

正题 luogu题目链接:https://www.luogu.org/problemnew/show/CF666B 题目大意 求4个点&#xff0c;使得这4个点按顺序最短路到达的长度最远。 解题思路 用bfsbfsbfs求出每个点之间的最短路&#xff0c;然后对于每个点求出最远点&#xff0c;次远点&#xff0c;反最远点和反次…

vue-cli2、vue-cli3脚手架详细讲解

转载自 vue-cli2、vue-cli3脚手架详细讲解 前言&#xff1a; vue脚手架指的是vue-cli它是vue官方提供的一个快速构建单页面&#xff08;SPA&#xff09;环境配置的工具&#xff0c;cli 就是(command-line-interface ) 命令行界面 。vue-cli是基于node环境利用webpack对文件进…

微软为.NET程序员带来了最优的跨平台开发体验-WSL

前言 在前几个Visual Studio Code更新中发现有一个重要得特性&#xff0c;就是nodejs可以使用VS Code在WSL中进行Debug了&#xff08;WSL是指Win10中的Linux子系统&#xff09;,之前写过一篇文章是使用SSH对Linux环境进行Debug&#xff0c;此时的想法就是如果可以在WSL中直接对…

article之api文档

查 method:get http://127.0.0.1:8000/article 单条 http://127.0.0.1:8000/article/10 method:get 新增 http://127.0.0.1:8000/article method:post 修改 http://127.0.0.1:8000/article/10 method:put 删除 http://127.0.0.1:8000/article/3 method:delete ## 查询所有数据&…

jzoj4672-Graph Coloring【图论,模拟】

正题 题目大意 一张无向图&#xff0c;每条边有一个颜色(红或蓝)&#xff0c;可以选择点使得连接的边都取反&#xff0c;求至少要选多个点可以使得所有边的颜色相同。 解题思路 不难发现如果确定所有边的颜色&#xff0c;然后知道一个点的选择后就可以知道整个联通图的选择。…

Redis 性能问题分析

转载自 Redis 性能问题分析 在一些网络服务的系统中&#xff0c;Redis 的性能&#xff0c;可能是比 MySQL 等硬盘数据库的性能更重要的课题。比如微博&#xff0c;把热点微博[1]&#xff0c;最新的用户关系&#xff0c;都存储在 Redis 中&#xff0c;大量的查询击中 Redis&am…

谈谈微服务中的 API 网关(API Gateway)

前言 又是很久没写博客了&#xff0c;最近一段时间换了新工作&#xff0c;比较忙&#xff0c;所以没有抽出来太多的时间写给关注我的粉丝写一些干货了&#xff0c;就有人问我怎么最近没有更新博客了&#xff0c;在这里给大家抱歉。 那么&#xff0c;在本篇文章中&#xff0c;我…

laravel如何生成swagger接口文档

php artisan serve --host 0.0.0.0 php artisan serve --port 8080 地址&#xff1a; http://127.0.0.1/blogkjh/public/api/documentation 1、安装包 composer require darkaonline/l5-swagger 2、配置 php artisan vendor:publish --provider “L5Swagger\L5SwaggerService…

jzoj4673,CF578D-LCS again【统计,字符串,容斥】

正题 luoguluoguluogu题目链接:https://www.luogu.org/problemnew/show/CF578D 题目大意 求有多少个字符串TTT使得其和字符串SSS的LCSLCSLCS长度为∣S∣−1|S|-1∣S∣−1 解题思路 首先考虑挖一个空再填一个字母。 这样方案数为n∗n∗mn*n*mn∗n∗m 但是我们考虑aabaabaab这样…

OAuth2 实现单点登录 SSO

转载自 OAuth2 实现单点登录 SSO 1. 前言 技术这东西吧&#xff0c;看别人写的好像很简单似的&#xff0c;到自己去写的时候就各种问题&#xff0c;“一看就会&#xff0c;一做就错”。网上关于实现SSO的文章一大堆&#xff0c;但是当你真的照着写的时候就会发现根本不是那么…

Ocelot网关

Ocelot是一个.net core框架下的网关的开源项目&#xff0c;下图是官方给出的基础实现图&#xff0c;即把后台的多个服务统一到网关处&#xff0c;前端应用&#xff1a;桌面端&#xff0c;web端&#xff0c;app端都只用访问网关即可。 Ocelot的实现原理就是把客户端对网关的请求…

linux服务器部署laravel出现putenv() has been disabled for security reasons

putenv() has been disabled for security reasons 进入 www/serve/php/72/etc 找到 disable_functions后面的函数&#xff0c;删除 putenv() 如果报错 The Process class relies on proc_open, which is not available on your PHP installation. 删除 proc_open

欢乐纪中A组周六赛【2019.3.23】

前言 做A组被虐好惨 成绩 RankRankRank是有算别人的 RankRankRankPersonPersonPersonScoreScoreScoreAAABBBCCC1313132017WYC2017WYC2017WYC1901901909090901001001000001919192017HZB2017HZB2017HZB1101101101001001001010100002727272017XJQ2017XJQ2017XJQ10010010010010010…

百度OCR文字识别-身份证识别

简介 答应了园区大牛张善友 要写AI 的系列博客&#xff0c;所以开始了AI 系列之旅。 一、介绍 身份证识别 API 接口文档地址&#xff1a;http://ai.baidu.com/docs#/OCR-API/top 接口描述 用户向服务请求识别身份证&#xff0c;身份证识别包括正面和背面。 请求说明 请求示例…

Spring Boot Elasticsearch 入门

转载自 芋道 Spring Boot Elasticsearch 入门 1. 概述 如果胖友之前有用过 Elasticsearch 的话&#xff0c;可能有过被使用的 Elasticsearch 客户端版本搞死搞活。如果有&#xff0c;那么一起握个抓。所以&#xff0c;我们在文章的开始&#xff0c;先一起理一理这块。 Elas…

内存不足The following exception is caused by a lack of memory or swap, or not having swap

在linux执行以下三个命令即可 /bin/dd if/dev/zero of/var/swap.1 bs1M count1024 /sbin/mkswap /var/swap.1 /sbin/swapon /var/swap.1

P2513-[HAOI2009]逆序对数列【dp,前缀和】

正题 题目链接:https://www.luogu.org/problemnew/show/P2513 题目大意 求长度为nnn逆序对为kkk个的序列总数。 解题思路 设fi,jf_{i,j}fi,j​表示1∼i1\sim i1∼i的排列逆序对个数为jjj 然后显然:fi,j∑k1i−1fi−1,j−kf_{i,j}\sum_{k1}^{i-1}f_{i-1,j-k}fi,j​k1∑i−1​…

在.NET Core类库中使用EF Core迁移数据库到SQL Server

前言 如果大家刚使用EntityFramework Core作为ORM框架的话&#xff0c;想必都会遇到数据库迁移的一些问题。 起初我是在ASP.NET Core的Web项目中进行的&#xff0c;但后来发现放在此处并不是很合理&#xff0c;一些关于数据库的迁移&#xff0c;比如新增表&#xff0c;字段&…

Spring Boot MongoDB 入门

转载自 芋道 Spring Boot MongoDB 入门 1. 概述 可能有一些胖友对 MongoDB 不是很了解&#xff0c;这里我们引用一段介绍&#xff1a; FROM 《分布式文档存储数据库 MongoDB》 MongoDB 是一个介于关系数据库和非关系数据库之间的产品&#xff0c;是非关系数据库当中功能最…