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 }

猜你喜欢

转载自www.cnblogs.com/share23/p/9611345.html