Java自定義實現equals()方法
以常見的自定義Date類型為例,沒有經驗的朋友可能會覺得直接比較年月日即可,從而寫出以下的實現
public class MyDate implements Comparable<MyDate> {
private final int year;
private final int month;
private final int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public int compareTo(MyDate o) {
throw new NotImplementedException();
}
public boolean equals(Date that) {
if (this.day != that.day) {
return false;
}
if (this.month != that.month) {
return false;
}
if (this.year != that.year) {
return false;
}
return true;
}
}
但是想要健壯地實現equals()方法,上述代碼是不夠的,參考以下代碼
//定義為final類型:允許子類直接使用父類equals()方法是不安全的
public final class MyDate implements Comparable<MyDate> {
private final int year;
private final int month;
private final int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public int compareTo(MyDate o) {
throw new NotImplementedException();
}
@Override
//規定參數必須是Object類型
public boolean equals(Object obj) {
//檢查是否相同引用
if (obj == this) {
return true;
}
//檢查null
if (obj == null) {
return false;
}
//getClass()判斷的是准確的運行時類型,instanceof的類型可以是父類或接口
if (obj.getClass() != this.getClass()) {
return false;
}
//這里類型轉換一定是安全的
MyDate that = (MyDate) obj;
//確認關鍵字段都相等
if (this.day != that.day) {
return false;
}
if (this.month != that.month) {
return false;
}
if (this.year != that.year) {
return false;
}
return true;
}
}
自定義equals方法的套路
- 檢查是否是同一個引用,如果是,返回true
- 檢查null值,如果是,返回false
- 檢查類型是否相同,如果不同,返回false;如果相同,進行類型轉換
- 對每個關鍵字段進行比較:
4.1 如果字段是基本類型,使用==
4.2 如果字段是對象類型,使用對象的equals()方法
4.3 如果字段是個數組,比較數組的每個元素。可以考慮使用Arrays.equals(a,b)
(比較普通數組)或者Arrays.deepEquals(a,b)
(比較嵌套數組,例如int[][]),但不是a.equals(b)
建議
- 如果一個字段的值完全依賴其他字段的值,可以不用比較
- 優先比較最可能出現差異的字段
- 如果對象實現了
compareTo()
方法,可以直接拿來使用。例如x.compareTo(y) == 0