JAVA中重寫equals()方法的同時要重寫hashcode()方法,equalshashcode

來源:互聯網
上載者:User

JAVA中重寫equals()方法的同時要重寫hashcode()方法,equalshashcode

object對象中的 public boolean equals(Object obj),對於任何非Null 參考值 x 和 y,若且唯若 x 和 y 引用同一個對象時,此方法才返回 true;
注意:當此方法被重寫時,通常有必要重寫 hashCode 方法,以維護 hashCode 方法的常規協定,該協定聲明相等對象必須具有相等的雜湊碼。如下:
(1)當obj1.equals(obj2)為true時,obj1.hashCode() == obj2.hashCode()必須為true 
(2)當obj1.hashCode() == obj2.hashCode()為false時,obj1.equals(obj2)必須為false
如果不重寫equals,那麼比較的將是對象的引用是否指向同一塊記憶體位址,重寫之後目的是為了比較兩個對象的value值是否相等。特別指出利用equals比較八大封裝對象
(如int,float等)和String類(因為該類已重寫了equals和hashcode方法)對象時,預設比較的是值,在比較其它自訂對象時都是比較的引用地址
hashcode是用於散列資料的快速存取,如利用HashSet/HashMap/Hashtable類來儲存資料時,都是根據儲存物件的hashcode值來進行判斷是否相同的。
這樣如果我們對一個對象重寫了euqals,意思是只要對象的成員變數值都相等那麼euqals就等於true,但不重寫hashcode,那麼我們再new一個新的對象,
當原對象.equals(新對象)等於true時,兩者的hashcode卻是不一樣的,由此將產生了理解的不一致,如在儲存散列集合時(如Set類),將會儲存了兩個值一樣的對象,
導致混淆,因此,就也需要重寫hashcode()
舉例說明: 

import java.util.*;public class HelloWorld {    public static void main(String[] args) {        /*        Collection c = new HashSet();        c.add("hello");        c.add(new Name("f1","l1"));        c.add(new Integer(100));        c.remove("hello");         c.remove(new Integer(100));        System.out.println(c.remove(new Name("f1","l1")));        */        Name n1 = new Name("01");        Name n2 = new Name("01");                Collection c = new HashSet();        c.add(n1);        System.out.println("------------");        c.add(n2);        System.out.println("------------");        System.out.println(n1.equals(n2));        System.out.println("------------");        System.out.println(n1.hashCode());        System.out.println(n2.hashCode());        System.out.println(c);    }}class Name {    private String id;    public Name(String id) {        this.id = id;     }        public String toString(){        return this.id;    }    public boolean equals(Object obj) {        if (obj instanceof Name) {            Name name = (Name) obj;            System.out.println("equal"+ name.id);            return (id.equals(name.id));        }        return super.equals(obj);    }            public int hashCode() {        Name name = (Name) this;        System.out.println("Hash" + name.id);        return id.hashCode();                }}

 

 

就這個程式進行分析,在第一次添加時,調用了hashcode()方法,將hashcode存入對象中,第二次也一樣,然後對hashcode進行比較。hashcode也只用於HashSet/HashMap/Hashtable類儲存資料,所以會用於比較,需要重寫
總結,自訂類要重寫equals方法來進行等值比較,自訂類要重寫compareTo方法來進行不同對象大小的比較,重寫hashcode方法為了將資料存入HashSet/HashMap/Hashtable類時進行比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.