重寫Object.equals()方法和Object.hashCode()方法

weixin_34119545發表於2012-05-12

參考文獻:

Java:重寫equals()和hashCode()

為什麼重寫equals方法,一定要重寫HashCode方法?

重寫equals和hashCode

《CoreJava I 8》第5.2節——Object:所有類的超類。

正文

 

程式碼示例

View Code
package v1ch05.EqualsTest;

import java.util.*;

/**
 * This program demonstrates the equals method.
 * @version 1.11 2004-02-21
 * @author Cay Horstmann
 */
public class EqualsTest
{
   public static void main(String[] args)
   {
      Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
      Employee alice2 = alice1;//alice1和alice2同引用
      Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);//alice3與alice1的資料內容相同。
      Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);

      System.out.println("alice1 == alice2: " + (alice1 == alice2));//因為兩者同引用,因此使用“==”返回true

      System.out.println("alice1 == alice3: " + (alice1 == alice3));//兩者不同引用,棧地址不相等,但是堆記憶體中的資料內容相等,因此"=="為false

      System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));//Employee重寫equals方法,因此返回true

      System.out.println("alice1.equals(bob): " + alice1.equals(bob));//兩者資料內容不同,因此為false

      System.out.println("bob.toString(): " + bob);//Employee重寫了toString()

      Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      boss.setBonus(5000);
      System.out.println("boss.toString(): " + boss);
      System.out.println("carl.equals(boss): " + carl.equals(boss));//因此資料域bonus不相等,所以返回false
      System.out.println("alice1.hashCode(): " + alice1.hashCode());//可以看到alice1與alice3的hashCode是相等的。
      System.out.println("alice3.hashCode(): " + alice3.hashCode());
      System.out.println("bob.hashCode(): " + bob.hashCode());//bob和carl的hashCode則不相等。
      System.out.println("carl.hashCode(): " + carl.hashCode());
   }
}

class Employee
{
    //建構函式,姓名,薪水,入職的年,月,日。
   public Employee(String n, double s, int year, int month, int day)
   {
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      hireDay = calendar.getTime();
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public Date getHireDay()
   {
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }

   //重寫equals方法
   public boolean equals(Object otherObject)
   {
      // 判讀當前物件與傳入進來的物件是否同引用,如果是則返回true,Object也是進行這樣的判斷
      if (this == otherObject) return true;

      // 如果傳入的物件時null則返回false
      if (otherObject == null) return false;

      // 如果當前物件和傳入的物件不屬於同一個類,那麼直接返回false
      if (getClass() != otherObject.getClass()) return false;

      // 到此我們可以確定傳入的物件是一個非空的Employee類,因此可以進行型別轉換
      Employee other = (Employee) otherObject;

      // 檢測資料域是否相等,當所有資料域相等時返回true
      return name.equals(other.name) && salary == other.salary && hireDay.equals(other.hireDay);
   }

   // 因此重寫了equals方法,所以必須重寫hashCode方法,因此必須保證如果兩個物件是equal的,那麼那麼他們的hashCode就必須是相等的。
   // 在類似Hashtable這樣的資料結構中,就是通過hashCode來進行判斷物件是否相等的,如果不重寫hashCode,那麼在Hashtable這樣的資料結構中
   // 即使兩個物件equal,它也會因為hashCode不同而錯誤得認為物件不相等。
   public int hashCode()
   {
      return 7 * name.hashCode() + 11 * new Double(salary).hashCode() + 13 * hireDay.hashCode();
   }

   public String toString()
   {
      return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay
            + "]";
   }

   private String name;
   private double salary;
   private Date hireDay;
}

class Manager extends Employee
{
   public Manager(String n, double s, int year, int month, int day)
   {
      super(n, s, year, month, day);
      bonus = 0;
   }

   public double getSalary()
   {
      double baseSalary = super.getSalary();
      return baseSalary + bonus;
   }

   public void setBonus(double b)
   {
      bonus = b;
   }

   public boolean equals(Object otherObject)
   {
      // 因為Manager是Employee的子類,所以可以先呼叫父類的equals方法進行比較
      // 如果父類的equal返回false,則直接返回,如果返回true,則在進行子類資料域的相等檢測。
      if (!super.equals(otherObject)) return false;
      Manager other = (Manager) otherObject;
      // 檢測子類Manager新增資料域是否相等,如果相等則返回true
      return bonus == other.bonus;
   }

   public int hashCode()
   {
      return super.hashCode() + 17 * new Double(bonus).hashCode();
   }

   public String toString()
   {
      return super.toString() + "[bonus=" + bonus + "]";
   }

   private double bonus;
}

 

 

相關文章