当前位置:网站首页>lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
2022-06-28 05:56:00 【小手cool】
实体类
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode
public class Cs {
private int id;
private String name;
public Cs(int id, String name) {
this.id = id;
this.name = name;
}
}
运行
public static void main(String[] args) {
Cs cs1=new Cs(1,"小小");
Cs cs2=new Cs(1,"小小");
System.out.println(cs1.equals(cs2));
}
查看生成的class文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.hieasy.rftools.common;
public class Cs {
private int id;
private String name;
public Cs(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Cs(id=" + this.getId() + ", name=" + this.getName() + ")";
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Cs)) {
return false;
} else {
Cs other = (Cs)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId() != other.getId()) {
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;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Cs;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getId();
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
}
如果我们去掉注解是一样的,全部比较的话可以不要@EqualsAndHashCode注解
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.hieasy.rftools.common;
public class Cs {
private int id;
private String name;
public Cs(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Cs)) {
return false;
} else {
Cs other = (Cs)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId() != other.getId()) {
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;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Cs;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 59 + this.getId();
Object $name = this.getName();
result = result * 59 + ($name == null ? 43 : $name.hashCode());
return result;
}
public String toString() {
return "Cs(id=" + this.getId() + ", name=" + this.getName() + ")";
}
}
但是我们如果只想比较name属性的话,类上需要加入@EqualsAndHashCode(onlyExplicitlyIncluded = true),想只比较哪些属性就在属性上加@EqualsAndHashCode.Include
import lombok.Data;
import lombok.EqualsAndHashCode;
/** * @author Zc * @date 2022年06月24日 16:04 */
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Cs {
private int id;
@EqualsAndHashCode.Include
private String name;
public Cs(int id, String name) {
this.id = id;
this.name = name;
}
}
结果
边栏推荐
- Academic search related papers
- At first glance, I can see several methods used by motionlayout
- Simple handwritten debounce function
- Syn retransmission caused by IPVS
- JSP connecting Oracle to realize login and registration
- Typescript base type
- 使用SSM框架,配置多个数据库连接
- Bidirectional level conversion circuit
- Data center: Seven Swords of data governance
- [untitled]
猜你喜欢

Oracle condition, circular statement

函数栈帧的创建和销毁

What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?

FB、WhatsApp群发消息在2022年到底有多热门?

Sklearn Feature Engineering (summary)

Capacity scheduling absolute value configuration queue usage and pit avoidance

原动力×云原生正发声 降本增效大讲堂

RL 实践(0)—— 及第平台辛丑年冬赛季【Rule-based policy】

What is the e-commerce conversion rate so abstract?

JSP
随机推荐
Cryptography notes
qtcanpool 知 07:Ribbon
Binder面试之:内存管理单元
JSP connects with Oracle to realize login and registration (simple)
数据中台:一篇带你深入浅出了解数据中台
ERP软件公司选型的重要根据
Filecoin hacker song developer competition
Qtcanpool knowledge 07:ribbon
File foundation - read / write, storage
Data warehouse: financial / banking theme layer division scheme
6. graduation design temperature and humidity monitoring system (esp8266 + DHT11 +oled real-time upload temperature and humidity data to the public network server and display the real-time temperature
1404. number of steps to reduce binary representation to 1
Ethereum Classic的难度计算|猿创征文
5GgNB和ng-eNB的主要功能
独立站卖家都在用的五大电子邮件营销技巧,你知道吗?
Data midrange: implementation and summary of AI midrange
pytorch详解
Error: the following arguments are required:
6. 毕业设计温湿度监控系统(ESP8266 + DHT11 +OLED 实时上传温湿度数据给公网服务器并在OLED显示屏上显示实时温湿度)
联想混合云Lenovo xCloud,新企业IT服务门户