实体类
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;
}
}
结果