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

平时开发过程中,有时需要重写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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值