当前位置:网站首页>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;
}
}
结果
边栏推荐
- 数据仓库:分层设计详解
- Jenkins continuous integration 1
- Yin Yang master page
- Lenovo hybrid cloud Lenovo xcloud, new enterprise IT service portal
- MR-WordCount
- Sklearn Feature Engineering (summary)
- Openharmony gnawing paper growth plan -- json-rpc
- Oracle condition, circular statement
- RL 实践(0)—— 及第平台辛丑年冬赛季【Rule-based policy】
- Ethereum Classic的难度计算|猿创征文
猜你喜欢

Jenkins continuous integration 1

联想混合云Lenovo xCloud,新企业IT服务门户

Ethereum Classic的难度计算|猿创征文
![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]

Apple MDM bypass jailfree bypass MDM configuration lock free

Openharmony gnawing paper growth plan -- json-rpc

Linked list in JS (including leetcode examples) < continuous update ~>

The windows environment redis uses AOF persistence and cannot generate an AOF file. After generation, the content of the AOF file cannot be loaded

How to add live chat in your Shopify store?

什么是WebRTC?
随机推荐
Xcode13.3.1 项目执行pod install后报错
Enum
Typescript base type
JSP connects with Oracle to realize login and registration (simple)
简单手写debounce函数
Qtcanpool knowledge 07:ribbon
Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
Qtcanpool q05: no border
【无标题】
Filecoin黑客松开发者大赛
Jenkins continues integration 2
JSP
Data warehouse: detailed explanation of hierarchical design
PKG package node project (express)
5g network overall architecture
数据仓库:DWS层设计原则
Install fmpefg
[CAD drawing Video] AutoCAD 2014 master's way
YYGH-BUG-02
Linked list in JS (including leetcode examples) < continuous update ~>