通过Lombok学习如何写好hashCode和equals方法

开发中重写JavaBean的hashCode和equals方法时,常不知如何编写。可借助Lombok的@Data自动填充这两个方法,通过jd - gui.exe反编译class字节码,能学习Lombok的重写方式,还给出了hashCode计算公式及不同类型变量hashCode值的获取方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平时开发过程中,有时需要重写javaBean的hashCode和equals方法,但是一时间却不知道如何编写,百度上一搜索各种写法,总感觉没一个是标准写法。还好,Lombok的@Data会自动给JavaBean填充hashCode和equals方法,可以通过反编译出来,学习下Lombok如何重写hashCode和equals方法的。

import lombok.Data;

import java.util.List;

/**
 * @author sunchangtan
 * @date 2019/2/15 13:18
 */
@Data
public class Person {
    private String name;
    private int age;
    private List<Friend> friendList;
    private Person[] parents;
    private Nation nation;
}

使用jd-gui.exe反编译出class字节码,代码如下:

package com.sample.mybatis.test;

import java.util.Arrays;
import java.util.List;

public class Person
{
  private String name;
  private int age;
  private List<Friend> friendList;
  private Person[] parents;
  private Nation nation;
  
  public void setAge(int age)
  {
    this.age = age;
  }
  
  public int hashCode()
  {
    int PRIME = 59;int result = 1;Object $name = getName();result = result * 59 + ($name == null ? 43 : $name.hashCode());result = result * 59 + getAge();Object $friendList = getFriendList();result = result * 59 + ($friendList == null ? 43 : $friendList.hashCode());result = result * 59 + Arrays.deepHashCode(getParents());Object $nation = getNation();result = result * 59 + ($nation == null ? 43 : $nation.hashCode());return result;
  }
  
  protected boolean canEqual(Object other)
  {
    return other instanceof Person;
  }
  
  public boolean equals(Object o)
  {
    if (o == this) {
      return true;
    }
    if (!(o instanceof Person)) {
      return false;
    }
    Person other = (Person)o;
    if (!other.canEqual(this)) {
      return false;
    }
    Object this$name = getName();Object other$name = other.getName();
    if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
      return false;
    }
    if (getAge() != other.getAge()) {
      return false;
    }
    Object this$friendList = getFriendList();Object other$friendList = other.getFriendList();
    if (this$friendList == null ? other$friendList != null : !this$friendList.equals(other$friendList)) {
      return false;
    }
    if (!Arrays.deepEquals(getParents(), other.getParents())) {
      return false;
    }
    Object this$nation = getNation();Object other$nation = other.getNation();return this$nation == null ? other$nation == null : this$nation.equals(other$nation);
  }
  
  public void setNation(Nation nation)
  {
    this.nation = nation;
  }
  
  public void setParents(Person[] parents)
  {
    this.parents = parents;
  }
  
  public void setName(String name)
  {
    this.name = name;
  }
  
  public String toString()
  {
    return "Person(name=" + getName() + ", age=" + getAge() + ", friendList=" + getFriendList() + ", parents=" + Arrays.deepToString(getParents()) + ", nation=" + getNation() + ")";
  }
  
  public void setFriendList(List<Friend> friendList)
  {
    this.friendList = friendList;
  }
  
  public String getName()
  {
    return this.name;
  }
  
  public int getAge()
  {
    return this.age;
  }
  
  public List<Friend> getFriendList()
  {
    return this.friendList;
  }
  
  public Person[] getParents()
  {
    return this.parents;
  }
  
  public Nation getNation()
  {
    return this.nation;
  }
  
  public static void main(String[] args) {}
}

hashCode方法:

从上述代码中,可整理下hashCode公式

 public int hashCode() {
        int PRIME = 59;
        int result = 1;
        Object $name = getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        result = result * 59 + getAge();
        long $height = Double.doubleToLongBits(getHeight());
        result = result * 59 + (int) ($height >>> 32 ^ $height);
        Object $friendList = getFriendList();
        result = result * 59 + ($friendList == null ? 43 : $friendList.hashCode());
        result = result * 59 + Arrays.deepHashCode(getParents());
        Object $nation = getNation();
        result = result * 59 + ($nation == null ? 43 : $nation.hashCode());
        return result;
    }
  1. hashCode计算公式:((59+第一个变量的hashCode) * 59 + 第二个变量的hashCode) * 59 + 第三个变量的hashCode + 。。。 以此类推

  2. int、long变量的hashCode值: 变量值

  3. double、float变量的hashCode值:需要特殊除了,先转换为longBits,比如Double.doubleToLongBits, 再(int) ($height >>> 32 ^ $height)

  4. 对象型变量的hashCode值:调用hashCode()方法获取

  5. 数组型变量的hashCode值:调用Arrays.deepHashCode()方法获取

  6. 变量是null: 默认是取43

equals方法:

protected boolean canEqual(Object other) {
        return other instanceof Person;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Person)) {
            return false;
        }
        Person other = (Person) o;
        if (!other.canEqual(this)) {
            return false;
        }
        Object this$name = getName();
        Object other$name = other.getName();
        
        //字符串变量的比较
        if (this$name == null ? other$name != null : !this$name.equals(other$name)) {
            return false;
        }
        
        //整型变量的比较
        if (getAge() != other.getAge()) {
            return false;
        }
        
        //浮点型变量比较
        if (Double.compare(getHeight(), other.getHeight()) != 0) {
            return false;
        }
        Object this$friendList = getFriendList();
        Object other$friendList = other.getFriendList();
        
        // 集合对象的变量比较
        if (this$friendList == null ? other$friendList != null : !this$friendList.equals(other$friendList)) {
            return false;
        }
        //数组型变量的比较
        if (!Arrays.deepEquals(getParents(), other.getParents())) {
            return false;
        }
        Object this$nation = getNation();
        Object other$nation = other.getNation();
        
        //一般对象的变量比较
        return this$nation == null ? other$nation == null : this$nation.equals(other$nation);
    }
<think>好的,用户的问题是关于Java中hashCodeequals方法的处理规则,需要详细解释为什么必须同时覆盖这两个方法,特别是在使用Set自定义对象作为Map键的时候。首先,我得回忆一下这两个方法的作用以及它们之间的关系。 首先,equals方法用于判断两个对象是否在逻辑上相等,而hashCode方法返回对象的哈希码,用于哈希表这样的数据结构,比如HashMap或HashSet。根据Java规范,如果两个对象通过equals方法比较是相等的,那么它们的hashCode必须相同。反过来,如果hashCode不同,那么这两个对象肯定不相等。但如果hashCode相同,equals不一定相等,这时候会进一步用equals判断。 用户提到的第一条规则是覆盖equals时必须覆盖hashCode。这是因为如果不这样做,当对象被放入基于哈希的集合(如HashSet)时,可能会出现错误。例如,假设两个对象在equals上是相等的,但hashCode不同,那么它们会被存储到哈希表的不同位置,导致Set中出现重复元素,违反了Set的不重复性原则。所以必须确保相等的对象有相同的哈希码。 第二条规则指出Set存储的对象必须覆盖这两个方法。因为Set的实现类如HashSet在添加元素时,首先检查hashCode是否相同,如果不同则直接存储;如果相同,则再调用equals方法判断是否真的相等。如果这两个方法没有被正确覆盖,Set可能无法正确识别重复元素,导致数据不一致。 第三条规则是关于自定义对象作为Map的键。Map的键需要唯一的标识,所以当使用自定义对象时,必须正确实现hashCodeequals,否则无法正确查找或存储键值对。例如,如果两个键对象在逻辑上相等但hashCode不同,它们会被视为不同的键,导致Map中出现重复键,而实际上应该覆盖相同的键。 接下来,我需要给出具体的代码示例来说明如何正确覆盖这两个方法。比如,一个User类,有idname属性。在覆盖equals时,要比较所有相关字段;覆盖hashCode时,使用Objects.hash方法将相同字段组合起来生成哈希码。这样,当两个User对象的idname都相等时,它们的哈希码也相等,符合规范。 然后,解释如果不这样做会发生的问题。例如,将User对象存入HashSet时,即使equals返回true,但hashCode不同,导致重复元素被添加,破坏了Set的特性。同样,在HashMap中使用这样的对象作为键,会导致无法正确检索值。 最后,提出相关问题,确保覆盖了常见的疑问点,比如为什么hashCodeequals要一起覆盖,Set如何判断重复,哈希冲突如何处理,以及String类在这方面的处理等。这些问题可以帮助用户进一步巩固理解。</think><< 在Java中,`hashCode``equals`方法的正确覆是保证对象在哈希集合(如`HashSet`、`HashMap`)中行为正确的关键。以下是具体规则的解释实现示例: --- ### 1. 覆`equals`时必须覆`hashCode` **原因**: Java规定,如果两个对象通过`equals`方法判断为相等,它们的`hashCode`必须相同。否则,当对象被用于哈希表(如`HashSet`或`HashMap`)时,会出现逻辑错误。例如: - 两个对象逻辑相等但哈希码不同,会被哈希表错误地分配到不同位置,导致无法正确去重或检索。 **代码示例**: ```java public class User { private int id; private String name; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return id == user.id && Objects.equals(name, user.name); } @Override public int hashCode() { return Objects.hash(id, name); // 必须保证equals为true时,hashCode相同 } } ``` --- ### 2. `Set`存储的对象必须覆`hashCode``equals` **原因**: `HashSet`内部依赖`HashMap`实现,判断元素是否重复的流程如下: 1. 比较两个对象的`hashCode`是否相同。 2. 如果哈希码相同,再调用`equals`判断是否逻辑相等。 如果未覆这两个方法: - 默认的`hashCode`基于内存地址生成,即使逻辑相等的对象也会被误判为不同。 - `Set`无法正确去重,导致存储重复元素。 --- ### 3. 自定义对象作为`Map`的键时需覆`hashCode``equals` **原因**: `Map`通过键的哈希码定位存储位置(桶),并通过`equals`确认键的唯一性。若未正确覆: - 相同的逻辑键可能被分配到不同桶,导致无法正确检索值。 - 不同逻辑键可能因哈希冲突被误判为相同,覆盖原有数据。 **示例问题**: ```java Map<User, String> map = new HashMap<>(); User u1 = new User(1, "Alice"); User u2 = new User(1, "Alice"); // 逻辑上相等但未覆hashCode/equals map.put(u1, "Value1"); map.get(u2); // 返回null,因为u1u2的默认hashCode不同 ``` --- ### 总结 - **覆`equals`的逻辑**:比较对象的所有关键字段。 - **覆`hashCode`的逻辑**:使用相同的字段生成哈希码(如`Objects.hash(field1, field2)`)。 - **验证工具**:可通过IDE(如IntelliJ)自动生成这两个方法,或使用`Lombok`的`@EqualsAndHashCode`注解。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值