当前位置:网站首页>Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes
Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes
2022-06-28 05:58:00 【Small hand Cool】
Entity class
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;
}
}
function
public static void main(String[] args) {
Cs cs1=new Cs(1," Little ");
Cs cs2=new Cs(1," Little ");
System.out.println(cs1.equals(cs2));
}
View the generated class file
//
// 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;
}
}
If we remove the annotation, it is the same , If you compare them all, you can not @EqualsAndHashCode annotation
//
// 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() + ")";
}
}
But if we just want to compare name Attribute words , Class needs to be added @EqualsAndHashCode(onlyExplicitlyIncluded = true), If you want to compare only those attributes, add @EqualsAndHashCode.Include
import lombok.Data;
import lombok.EqualsAndHashCode;
/** * @author Zc * @date 2022 year 06 month 24 Japan 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;
}
}
result 
边栏推荐
- ERP软件公司选型的重要根据
- 什么是WebRTC?
- 数据仓库:金融/银行业主题层划分方案
- [CAD drawing Video] AutoCAD 2014 master's way
- Data middle office: six questions data middle office
- Oracle condition, circular statement
- Data warehouse: financial / banking theme layer division scheme
- bash install.sh ********错误
- Global country (and region) information JSON data
- Error: the following arguments are required:
猜你喜欢

Deep learning 19 loss functions

Capacity scheduling absolute value configuration queue usage and pit avoidance
![A full set of excellent SEO tutorials worth 300 yuan [159 lessons]](/img/d7/7e522143b1e6b3acf14a0894f50d26.jpg)
A full set of excellent SEO tutorials worth 300 yuan [159 lessons]

Where is the era bonus for developers?

开发者的时代红利在哪里?

数据中台:六问数据中台

Drop down box for implementation

高质量国产立体声编解码器CJC8988,Pin to Pin替代WM8988

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

JSP
随机推荐
Official answers to the "network security" competition questions of the 2022 national vocational college skills competition
Flink window mechanism (two waits and the last explanation)
Xcode13.3.1 项目执行pod install后报错
Simple handwritten debounce function
Enum
Sharing tips for efficient scripting
The length of pytorch dataloader the difference between epoch and iteration
从0配置redis
YYGH-6-微信登录
Academic search related papers
Ape pink ape power - Developer activity attack!
ipvs 导致syn 重传问题
密码学笔记
@Autowired注解为空的原因
Main functions of 5ggnb and ng ENB
数据仓库:分层设计详解
Relevant implementation records of CSI and local disk
Filecoin黑客松开发者大赛
Openharmony gnawing paper growth plan -- json-rpc
数据中台:六问数据中台