天天看點

Object 方法的 hashCode,equals方法源碼

文章目錄

    • hashCode方法注釋
    • equals 方法注釋
    • equals 方法

hashCode方法注釋

Object 的 hashCode 方法,是本地方法;

Returns a hash code value for the object. This method is

* supported for the benefit of hash tables such as those provided by

* {@link java.util.HashMap}.

注釋如是說:提供此方法支援的原因是,為了支援一些 hash tables ;

Whenever it is invoked on the same object more than once during

* an execution of a Java application, the {@code hashCode} method

* must consistently return the same integer, provided no information

* used in {@code equals} comparisons on the object is modified.

* This integer need not remain consistent from one execution of an

* application to another execution of the same application.

在不修改

equals

方法的前提下,在一次程式執行期間,多次調用同一個對象的

hashCode

方法,得到的結果始終應該是一個相同的整數(

Integer

類型);當然,如果在不同的程式中,此結論不成立;

If two objects are equal according to the {@code equals(Object)} * method, then calling the {@code hashCode} method on each of * the two objects must produce the same integer result.

如果兩個對象的

equals

方法傳回值一樣,則調用它們各自的

hashCode

方法,傳回值也必須保證一樣;

It is not required that if two objects are unequal * according to the {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the {@code hashCode} method on each of the * two objects must produce distinct integer results. However, the * programmer should be aware that producing distinct integer results * for unequal objects may improve the performance of hash tables.

對于

equals

方法傳回值不同的對象,對其

hashCode

的傳回值沒有必須不同的要求,但是我們應該知道,對于

equals

方法傳回值不同的對象,其

hashCode

方法最好也設計成傳回不同的結果,這樣有助于提高 hash tables 的性能 ;

Object

對象的

hashCode

方法,不同的對象在調用的時候,确實傳回了不同的結果,得益于本地實作,傳回了對象的記憶體位址 ;

equals 方法注釋

It is reflexive: for any non-null reference value * {@code x}, {@code x.equals(x)} should return * {@code true}.

equals

方法具有 自反性,對于非

null

引用

x

, x.equals(x) 應該傳回

true

It is symmetric: for any non-null reference values * {@code x} and {@code y}, {@code x.equals(y)} * should return {@code true} if and only if * {@code y.equals(x)} returns {@code true}. x

equals

方法具有 對稱性,對于非

null

引用

x,y

, x.equals(y) 的傳回值應該和 y.equals(x) 的傳回值保持一緻;

It is transitive: for any non-null reference values * {@code x}, {@code y}, and {@code z}, if * {@code x.equals(y)} returns {@code true} and * {@code y.equals(z)} returns {@code true}, then * {@code x.equals(z)} should return {@code true}.

equals

方法具有 傳遞性,對于非

null

引用

x,y,z

,如果 x.equals(y) 傳回

true

, y.equals(z) 傳回值為

true

,則 x.equals(z) 也必須傳回

true

It is consistent: for any non-null reference values {@code x} and {@code y}, multiple invocations of {@code x.equals(y)} consistently return {@code true} or consistently return {@code false}, provided no information used in {@code equals} comparisons on the objects is modified.

equals

方法具有 一緻性,對于非

null

引用

x,y

,在沒有修改

equals

方法的前提下,多次調用 x.equals(y) 的傳回值,應該和多次調用 y.equals(z) 的傳回值保持一緻;

For any non-null reference value {@code x}, * {@code x.equals(null)} should return {@code false}

對于任何的非

null

引用

x

,x.equals(null) 傳回

false

;

Note that it is generally necessary to override the {@code hashCode} method whenever this method is overridden, so as to maintain the general contract for the {@code hashCode} method, which states that equal objects must have equal hash codes.

記住了!我們在覆寫

equals

方法的時候,最好,也同時覆寫

hashCode

,以便保證 equals 方法傳回值相同的對象,hashCode方法傳回值也相同 ;

equals 方法

public boolean equals(Object obj) {
        return (this == obj);
    }
           

Object

類的

equals

方法,内部隻是簡單的判斷對象的位址 ;

  • List item