文章目录
lombok(三)
注解
EqualsAndHashCode
减少编写equals和hashCode方法,自动生成对应的方法
EqualsAndHashCode理解
equals和hashCode方法本质上是一起使用的
自动生成了equals、canEqual、hashCode方法
@EqualsAndHashCode
public class User{
private String uid;
private String uname;
}
public class User {
private String uid;
private String uname;
public User() {
}
public boolean equals(Object o) {
//1.判断地址是否相同
if (o == this) {
return true;
//2.判断类型是否相同
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
//3.判断类型是否相等?
if (!other.canEqual(this)) {
return false;
} else {
//4.之后匹配成员变量是否相等
Object this$uid = this.uid;
Object other$uid = other.uid;
if (this$uid == null) {
if (other$uid != null) {
return false;
}
} else if (!this$uid.equals(other$uid)) {
return false;
}
Object this$uname = this.uname;
Object other$uname = other.uname;
if (this$uname == null) {
if (other$uname != null) {
return false;
}
} else if (!this$uname.equals(other$uname)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
//根据成员变量来生成对应hash值
public int hashCode() {
int PRIME = true;
int result = 1;
Object $uid = this.uid;
int result = result * 59 + ($uid == null ? 43 : $uid.hashCode());
Object $uname = this.uname;
result = result * 59 + ($uname == null ? 43 : $uname.hashCode());
return result;
}
}
使用范围
使用范围 | 使用后结果文字描述 |
---|---|
TYPE(类上) | 对添加的注解的类,在编译时生成equals和hashcode方法 |
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface EqualsAndHashCode {}
EqualsAndHashCode的属性
属性 | 入参 | 属性含义 |
---|---|---|
exclude | String[] | 传入的类容将不会在equals方法中进行对比,及上面equals中第4步不会排除的成员对象进行比较 |
of(与exclude互斥) | String[] | 传入的类容将会在equals方法中进行对比,及上面equals中第4步只会对传入的成员对象进行比较 |
callSuper | boolean,默认false | 被注解的类生成的equals和hashCode方法中会增加父类的equals和hashCode方法进行判断 |
doNotUseGetters | boolean,默认false | 当存在set方法时,且doNotUseGetters=true,equals和hashCode生成将使用this.xxx的形式获取参数,doNotUseGetters=false时生成toString使用this.getxxx的方式.官方说避免使用直接调用属性的方式(this.xxx) |
cacheStrategy | CacheStrategy(枚举,包含NEVER、LAZY),默认CacheStrategy.NEVER | hashCode方法的是否需要缓存,及是否每次都是重新计算hash值,当CacheStrategy.NEVER时每次调用都重新计算,CacheStrategy.LAZY则会进行缓存.注意:因为hashCode方法计算时使用成员的是成员变量进行计算的,当变量都是不可变时,使用缓存机制会提高性能,但是如果变化,那么就会出现改成员变量后equals出现问题 |
onlyExplicitlyIncluded(只包含明确字段) | boolean,默认false | 生成的eqluas/hashCode方法只包含显示标记@ToString.Include的字段,ture为包含所有非静态字段,false为不包含非静态字段配合@EqualsAndHashCode.Include和@EqualsAndHashCode.Exclude使用 |
@EqualsAndHashCode.Include
此注解只能配合@EqualsAndHashCode或者@Data一起使用,当注解在成员变量时,注解只能配合@ToString中的onlyExplicitlyIncluded=true属性一起使用使用
使用范围
使用范围 | 使用范围描述 |
---|---|
FIELD | 当onlyExplicitlyIncluded=true时,将该成员变量作为eqluas/hashCode方法计算的一部分 |
METHOD | 将该方法的返回值作为eqluas/hashCode方法计算的一部分 |
属性
属性 | 入参 | 属性含义 |
---|---|---|
replaces ?没有体会到用处 | String | 方法名和成员变量名字相同,则此属性替代方法名 |
rank | int默认为0 | 数值越大,作为eqluas/hashCode方法计算越靠前 |
@EqualsAndHashCode.Exclude
此注解只能配合@EqualsAndHashCode或者@Data一起使用,onlyExplicitlyIncluded=true时,被注解的成员变量不做为eqluas/hashCode方法计算的一部分
使用范围 | 使用范围描述 |
---|---|
FIELD | 当onlyExplicitlyIncluded=true时,将该成员变量不作为eqluas/hashCode方法计算的一部分 |
组合注解
@Data
@Data为组合注解,其包含了@Setter、@Getter、@ToString、@EqualsAndHashCode注解
使用范围
使用范围 | 使用范围描述 |
---|---|
TYPE 类上 | 被注解的类,就像被@Setter、@Getter、@ToString、@EqualsAndHashCode一起注解的一样 |
@Target(ElementType.TYPE)
//注解只保存在源码中
@Retention(RetentionPolicy.SOURCE)
public @interface Data {}
属性
属性 | 入参 | 属性含义 |
---|---|---|
staticConstructor(静态构造器) | String 默认为"" | 用于自动生成静态工厂方法,入参为静态工厂方法的方法名 |
@Data(staticConstructor = "staticUser")
public class User{
private String uid;
}
public class User{
private String uid;
private User() {
}
//生成的静态工厂方法
public static User staticUser() {
return new User();
}
public String getUid() {
return this.uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public boolean equals(Object o) {
***
}
protected boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
***
}
public String toString() {
return "User(uid=" + this.getUid() + ")";
}
}