HashCode和Equals區(qū)別(一)

我們首先來看一下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&trade; 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());
HashCode結(jié)果1.png

如果根據(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}方法的通用契約缨称,
該契約聲明相等的對象必須具有相等的散列代碼。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末祝迂,一起剝皮案震驚了整個濱河市睦尽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌型雳,老刑警劉巖当凡,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件山害,死亡現(xiàn)場離奇詭異,居然都是意外死亡沿量,警方通過查閱死者的電腦和手機浪慌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來朴则,“玉大人权纤,你說我怎么就攤上這事∥诙剩” “怎么了汹想?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長撤蚊。 經(jīng)常有香客問我古掏,道長,這世上最難降的妖魔是什么拴魄? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任冗茸,我火速辦了婚禮,結(jié)果婚禮上匹中,老公的妹妹穿的比我還像新娘夏漱。我一直安慰自己,他們只是感情好顶捷,可當(dāng)我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布挂绰。 她就那樣靜靜地躺著,像睡著了一般服赎。 火紅的嫁衣襯著肌膚如雪葵蒂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天重虑,我揣著相機與錄音践付,去河邊找鬼。 笑死缺厉,一個胖子當(dāng)著我的面吹牛永高,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播提针,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼命爬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了辐脖?” 一聲冷哼從身側(cè)響起饲宛,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嗜价,沒想到半個月后艇抠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體幕庐,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡悄晃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年段标,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谆甜。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡媒鼓,死狀恐怖届吁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情绿鸣,我是刑警寧澤疚沐,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站潮模,受9級特大地震影響亮蛔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜擎厢,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一究流、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧动遭,春花似錦芬探、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至宵蕉,卻和暖如春酝静,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背羡玛。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工别智, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人稼稿。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓亿遂,卻偏偏與公主長得像,于是被迫代替她去往敵國和親渺杉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容