当前位置:   article > 正文

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

lombook冲写的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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

使用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) {}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/650533
推荐阅读
相关标签
  

闽ICP备14008679号