重寫equals方法,重寫equals

來源:互聯網
上載者:User

重寫equals方法,重寫equals

 我們都知道,==是用來比較引用的(物理上的相等),而equals方法是用來比較值的(邏輯上的相等),在許多時候需要重寫equals方法來實現我們的需求,比如把對象放到容器中,然後去尋找對象。

在重寫equals 方法時要遵循一些契約:

  • 自反性:對於非Null 參考x而言,x.equals(x) 必須為true
  • 對稱性:對於非Null 參考x和y,如果x.equals(y)為true,那麼y.equals(x)必須也為true
  • 傳遞性:對於非Null 參考x,y和z,如果x.equals(y)為true,並且y.equals(z)為true,那麼x.equals(z)必須也為true
  • 持久性:對於非Null 參考x和y,如果x.equals(y) 為true,那麼當在x和y並未被修改的情況下,無論執行多少次x.equals(y),這個結果都為true
  • null-false: 對於非Null 參考x,x.equals(null) 始終為false

 

反例:

破壞自反性

1 public class AntiReflexive { 2 3 private String str; 4 public String getStr() { 5 return str; 6 } 7 8 public void setStr(String str) { 9 this.str = str;10 }11 12 public boolean equals(Object o) {13 if (!(o instanceof AntiReflexive)) {14 return false;15 }16 17 AntiReflexive ar = (AntiReflexive)o;18 return this.str.equals(ar.str.toLowerCase()); // tolowercase 導致了自反性的破壞19 }20 21 public static void main(String[] args) {22 AntiReflexive ar1 = new AntiReflexive();23 ar1.setStr("Hello world");24 25 System.out.println(ar1.equals(ar1));26 }27 }View Code

 

破壞對稱性

1 /** 2 * 代碼來自 effective java 3 */ 4 public class AntiSymmetric { 5 6 public static void main(String[] args) { 7 CaseInsensitiveString cis = new CaseInsensitiveString("abc"); 8 String str = "abc"; 9 System.out.println(cis.equals(str));10 System.out.println(str.equals(cis));11 }12 }13 14 class CaseInsensitiveString {15 16 private String s;17 18 public CaseInsensitiveString(String s) {19 this.s = s;20 }21 22 public boolean equals(Object o) {23 if (o instanceof CaseInsensitiveString) {24 return s.equalsIgnoreCase(((CaseInsensitiveString)o).s);25 }26 27 if (o instanceof String) { // 這個地方破壞了對稱性,因為在String中不存在這樣的邏輯,單向的28 return s.equalsIgnoreCase((String)o);29 }30 31 return false;32 }33 }View Code

 

破壞傳遞性

1 public class AntiTransitive { 2 3 public static void main(String[] args) { 4 ColorPoint x = new ColorPoint(10, 20, Color.red); 5 Point y = new Point(10, 20); 6 ColorPoint z = new ColorPoint(10, 20, Color.blue); 7 8 System.out.println("x.equals(y) = " + x.equals(y)); 9 System.out.println("y.equals(z) = " + y.equals(z));10 System.out.println("x.equals(z) = " + x.equals(z));11 }12 }13 14 class Point {15 private int x;16 private int y;17 18 public Point(int x, int y) {19 this.x = x;20 this.y = y;21 }22 23 public boolean equals(Object o) {24 if (!(o instanceof Point)) {25 return false;26 }27 28 Point p = (Point)o;29 30 return x == p.x && y == p.y;31 }32 }33 34 class ColorPoint extends Point {35 private Color color;36 37 public ColorPoint(int x, int y, Color color) {38 super(x, y);39 this.color = color;40 }41 42 public boolean equals(Object o) {43 if (!(o instanceof Point)) {44 return false;45 }46 47 if (!(o instanceof ColorPoint)) {48 return o.equals(this); // 這個地方破壞了傳遞性49 }50 51 ColorPoint cp = (ColorPoint)o;52 53 return super.equals(o) && cp.color == this.color;54 }55 }View Code

 

在編寫equals代碼時,一些好的習慣是非常必要的

  • 使用 == 判斷對象是否就等於this,這樣就保證了自反性,還提高了performance
  • 使用instanceof 來判斷這個對象是不是正確的類型,如果不是,就返回false
  • 將參數強制轉換為正確的類型,因為經過了instanceof的測試,索引放心的轉吧
  • 優先比較導致返回false的可能性比較大的欄位,只要發現不一樣就返回false
  • 對於float和double類型的欄位,使用Float.compare 或者 Double.compare 方法來比較
  • 對於數組欄位,遍曆數組比較其中的每一項
  • 不要比較冗餘欄位(比如多邊形類型的面積欄位,就不用比較) 
  • 如果重寫了equals,請重寫hashcode

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.