Java 重写 hashCode, 和 equals


1. hashCode

  1.1 基本概念

  hashCode 是 JDK 根据对象的地址算出来的一个 int 数字(对象的哈希码值),代表了该对象再内存中的存储位置。

  hashCode() 方法是超级类 Object类 提供的一个方法,所有类都可以对该方法进行重写。

  1.2 为什么重写 equals()方法 要重写 hashCode()方法

  hashCode() 相等是两个对象相等的必要非充分条件。

  equals() 相等是两个对象相等的充要条件。

  重写 equals()方法 一定要重写 hashCode()方法 是为了提升效率,初步通过 hashCode() 判断是否相等,相等之后再通过 equals() 中的别的方式判断是否相等。


2. equals()方法

  为保证程序健壮性,在重写 equals 方法时需满足以下情况:

  1. 自反性 : A.equals(A) 要返回 true
  2. 对称性:如果 A.equals(B) 返回 true,则 B.equals(A) 也要返回 true
  3. 传递性:如果 A.equals(B) 为 true,B.equals(C) 为 true,则 A.equals(C) 也要为 true, 相当于如果 A = B, B = C ,那么 A = C
  4. 一致性:只要 A、B 对象的状态没有改变,A.equals(B) 必须始终返回 true
  5. A.equals(null) 要返回 false

3. 练习

  设计一个类Person 含有 height、weight、age 和 blood 是整数属性,实现 hashcode 方法,将四个属性编排到一个整数中作为 hashCode() 和 equals()方法

 1 public class Person {
 2     public int height;
 3     public int weight;
 4     public int age;
 5     public int blood;
 6 
 7     public int getHeight() {
 8         return height;
 9     }
10 
11     public void setHeight(int height) {
12         this.height = height;
13     }
14 
15     public int getWeight() {
16         return weight;
17     }
18 
19     public void setWeight(int weight) {
20         this.weight = weight;
21     }
22 
23     public int getAge() {
24         return age;
25     }
26 
27     public void setAge(int age) {
28         this.age = age;
29     }
30 
31     public int getBlood() {
32         return blood;
33     }
34 
35     public void setBlood(int blood) {
36         this.blood = blood;
37     }
38 
39     public Person(int height, int weight, int age, int blood) {
40         this.height = height;
41         this.weight = weight;
42         this.age = age;
43         this.blood = blood;
44     }
45 
46     @Override
47     public boolean equals(Object o) {
48 
49         if(this.hashCode() == o.hashCode()){
50             return true;
51         }
52         return false;
53     }
54 
55     @Override
56     public int hashCode() {
57 
58         int i = (height << 24 & 0xff) | (weight << 16 & 0xff) | (age << 8 & 0xff) | blood;
59 
60         return i;
61     }
62 
63     public static void main(String[] args) {
64         Person p1 = new Person(1, 2, 3, 5);
65         Person p2 = new Person(1, 3, 3, 4);
66         System.out.println(p1.equals(p2));
67     }
68 
69 }