Lombok @EqualsAndHashCode 的用法

 

  1. 此注解会生成equals(Object other) 和 hashCode()方法。
  2. 它默认使用非静态,非瞬态的属性
  3. 可通过参数exclude排除一些属性
  4. 可通过参数of指定仅使用哪些属性
  5. 它默认仅使用该类中定义的属性且不调用父类的方法

当启动@EqualsAndHashCode时,默认不调用父类的equals方法,当做类型相等判断时,会遇到麻烦,例如:

@Data
public class People {
    private Integer id;
}

@Data
public class User extends People {
    private String name;
    private Integer age;
}

public static void main(String[] args) {
    User user1 = new User();
    user1.setName("jiangxp");
    user1.setAge(18);
    user1.setId(1);

    User user2 = new User();
    user2.setName("jiangxp");
    user2.setAge(18);
    user2.setId(2);

    System.out.println(user1.equals(user2));
}

输出结果:true

注意:两条user数据,ID完全不一样,结果明显是错的,没有做id的equals判断

需要将@EqualsAndHashCode修改为@EqualsAndHashCode(callSuper = true)才能得到正确结果.
反编译修改后的User.class,发现有些许变化

public boolean equals(Object o) {
    if (o == this) {
        return true;
    } else if (!(o instanceof User)) {
        return false;
    } else {
        User other = (User)o;
        if (!other.canEqual(this)) {
            return false;
        } else if (!super.equals(o)) {    // (1)此处变化,调用了父类的equals,原:无此段逻辑
            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;
            }

            Object this$age = this.getAge();
            Object other$age = other.getAge();
            if (this$age == null) {
                if (other$age != null) {
                    return false;
                }
            } else if (!this$age.equals(other$age)) {
                return false;
            }

            return true;
        }
    }
}

 public int hashCode() {
    int PRIME = true;
    int result = super.hashCode(); //(2)此处变化,调用了父类的hashCode(); 原:int result = 1;
    Object $name = this.getName();
    result = result * 59 + ($name == null ? 43 : $name.hashCode());
    Object $age = this.getAge();
    result = result * 59 + ($age == null ? 43 : $age.hashCode());
    return result;
}
`@EqualsAndHashCode` 是 Lombok 提供的一个注解,用于生成类的 equals() 和 hashCode() 方法,这在处理集合操作和数据库查询时尤其有用。它会检查类中哪些字段是对象标识的一部分(如主键),并据此计算 `equals()` 和 `hashCode()` 的返回值。 当在类上使用 `@EqualsAndHashCode` 注解时,Lombok 将自动添加对对象自身和其他类实例的引用作为比较的对象部分。默认情况下,这个方法会调用 super 类(如果存在的话)的 equals() 和 hashCode() 方法。 然而,在某些情况下,我们可能希望禁止从超类继承的 equals() 和 hashCode() 行为,而是仅基于类当前的状态进行比较。这就是 `callSuper = false` 这个属性派上了用场。当我们向 `@EqualsAndHashCode` 添加 `callSuper = false` 参数时,那么类将不再调用超类的 `equals()` 和 `hashCode()` 方法。 例如: ```java import lombok.EqualsAndHashCode; @EqualsAndHashCode(callSuper = false) public class MyCustomClass extends SomeBaseClass { // ... // 自定义 equals() 和 hashCode() 根据当前对象状态 } ``` 通过使用 `callSuper = false` 属性,我们可以确保 `MyCustomClass` 实现了自己的逻辑来进行比较,而不会受到其超类行为的影响。这种做法通常适用于那些想要根据自定义规则实现比较逻辑的情况,而不是仅仅依赖于基础类型的数据。 --- ### 相关问题: 1. 在什么情况下应该禁用 `@EqualsAndHashCode.callSuper`? 2. `@EqualsAndHashCode` 注解与其他注解(如 `@ToString`, `@Getter`, `@Setter` 等)一起使用时有何区别? 3. 如何使用 `@EqualsAndHashCode` 为某个字段提供特定的排除机制,使其不影响 `equals()` 和 `hashCode()` 的生成?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值