我們首先來看一下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}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>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.
* <li>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.
* <li>It is <em>not</em> 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.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java™ programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
public native int hashCode();
刨到這種地步郊供,也差不多了止后,我們看一下上面的注釋是啥意思钠至?
返回對象的Hash Code值葛虐,支持此方法的好處是可以使用{@link java.util.HashMap}.提供的哈希表
每當(dāng)在Java應(yīng)用程序執(zhí)行期間在同一個對象上多次調(diào)用該方法時,
{@code hashCode}方法必須一致地返回相同的整數(shù)棉钧,
前提是不修改對象上的{@code =}比較中使用的信息屿脐。
從應(yīng)用程序的一次執(zhí)行到同一應(yīng)用程序的另一次執(zhí)行,該整數(shù)不必保持一致宪卿。
String string = new String();
string="bbbb";
System.out.println(string.hashCode());
string="dddd";
System.out.println(string.hashCode());
如果根據(jù){@code equals(Object)}方法兩個對象相等的诵,
那么在兩個對象上調(diào)用{@code hashCode}方法必須產(chǎn)生相同的整數(shù)結(jié)果。
如果根據(jù){@link java.lang.Object#equals(java.lang.Object)}方法兩個對象不相等愧捕,
那么在兩個對象上調(diào)用{@code hashCode}方法必須產(chǎn)生不同的整數(shù)結(jié)果奢驯,
這是不必要的。但是次绘,程序員應(yīng)該意識到瘪阁,為不相等的對象生成不同的整數(shù)結(jié)果可能會提高哈希表的性能撒遣。
在相當(dāng)實際的情況下,類{@code Object}定義的hashCode方法確實為不同的對象返回不同的整數(shù)管跺。
(這通常是通過將對象的內(nèi)部地址轉(zhuǎn)換為整數(shù)來實現(xiàn)的义黎,
但是Java&trade并不需要這種實現(xiàn)技術(shù);編程語言)。
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The {@code equals} method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: 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}.
* <li>It is <i>transitive</i>: 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}.
* <li>It is <i>consistent</i>: 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.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* <p>
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
* 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.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}
1豁跑、{@code =}方法在非空對象引用上實現(xiàn)等價關(guān)系:
2廉涕、它是自反的:對于任何非空引用值{@code x},
{@code x.equals(x)}應(yīng)該返回{@code true}艇拍。
3狐蜕、它是對稱的:對于任何非空的引用值{@code x}和{@code y},
當(dāng)且僅當(dāng){@code y = (x)}返回{@code true}時卸夕,
{@code x.equals(y)}應(yīng)該返回{@code true}层释。
4、 它是傳遞的:對于任何非空的參考值{@code x}快集, {@code y}贡羔, {@code z},如果{@code x.equals(y)}返回{@code true}个初,
{@code y.equals(z)}返回{@code true}乖寒,那么{@code x.equals(z)}應(yīng)該返回{@code true}。
5院溺、它是一致的:對于任何非空引用值{@code x}和{@code y}楣嘁,對{@code x.equals(y)}的多次調(diào)用
一致返回{@code true}或一致返回{@code false},但在對象被修改珍逸。
6马澈、對于任何非空引用值{@code x}, {@code x.equals(null)}應(yīng)該返回{@code false}弄息。
7、{@code eauals}方法類{@codeObject}實現(xiàn)最歧視可能等價關(guān)系對象,也就是說,對于任何非空引用值{@code x}和
{@code y},該方法返回{@code true}當(dāng)且僅當(dāng){@code x}和{@code y}引用同一個對象({@code x = = y}
{@code true})的值勤婚。
8摹量、注意,通常需要在重寫該方法時重寫{@code hashCode}方法馒胆,以便維護{@code hashCode}方法的通用契約缨称,
該契約聲明相等的對象必須具有相等的散列代碼。