久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 HashMap.put 既比較哈希值又測試相等性?

Why does HashMap.put both compare hashes and test equality?(為什么 HashMap.put 既比較哈希值又測試相等性?)
本文介紹了為什么 HashMap.put 既比較哈希值又測試相等性?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我分析Java中的HashMap源代碼,得到一個關(guān)于put方法的問題.

I analyse the HashMap source code in Java and get a question about the put method.

下面是JDK1.6中的put方法:

Below is the put method in JDK1.6:

public V put(K key, V value) {
    if (key == null)
        return putForNullKey(value);
    int hash = hash(key.hashCode());
    int i = indexFor(hash, table.length);
    for (Entry<K,V> e = table[i]; e != null; e = e.next) {
        Object k;
        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }

    modCount++;
    addEntry(hash, key, value, i);
    return null;
}

我對 if (e.hash == hash && ((k = e.key) == key || key.equals(k))) 感到困惑

為什么會這樣?

因為在Java超類Object中,有一個contract of hashCode and equals:

Because in Java super class Object, there is a contract of hashCode and equals:

如果兩個對象根據(jù)equals(Object)方法相等,那么對兩個對象中的每一個調(diào)用hashCode方法必須產(chǎn)生相同的整數(shù)結(jié)果.

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

所以 key.equals(k) 意味著 key.hashCode() == k.hashCode().

hash()如下:

 static int hash(int h) {
    // This function ensures that hashCodes that differ only by
    // constant multiples at each bit position have a bounded
    // number of collisions (approximately 8 at default load factor).
    h ^= (h >>> 20) ^ (h >>> 12);
    return h ^ (h >>> 7) ^ (h >>> 4);
}

因此 key.hashCode() == k.hashCode() 意味著 e.hash == hash.

那么為什么不是if ((k = e.key) == key || key.equals(k))這樣的條件?

推薦答案

這只是一個優(yōu)化:比較兩個整數(shù)比調(diào)用 equals() 更快.

It's just an optimization: comparing two integers is faster than calling equals().

如果兩個hashcode不同,那么根據(jù)equalshashCode的約定,map知道現(xiàn)有的key不等于給定的key,并且可以更快地進(jìn)入下一個.

If the two hashcodes differ, then, based on the contract of equals and hashCode, the map knows that the existing key isn't equal to the given key, and can go faster to the next one.

這篇關(guān)于為什么 HashMap.put 既比較哈希值又測試相等性?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Couchbase Bucket authentication error(Couchbase 存儲桶身份驗證錯誤)
admob 6.2.1 nullpointer exception(admob 6.2.1 空指針異常)
How to setup SDK in IntelliJ IDEA?(如何在 IntelliJ IDEA 中設(shè)置 SDK?)
My phone cannot be detected in eclipse to test run(eclipse 無法檢測到我的手機進(jìn)行試運行)
platform-toolsaapt.exe directory missing in android SDK(android SDK 中缺少 platform-toolsaapt.exe 目錄)
error importing com.google.android.gms.*;(導(dǎo)入 com.google.android.gms.* 時出錯;)
主站蜘蛛池模板: 久久99精品久久久久久 | 免费看国产精品视频 | 福利视频网站 | 亚洲成人一区二区 | 美女一级毛片 | 国产农村一级国产农村 | 欧美日韩专区 | 亚洲精品高清视频在线观看 | 久久免费国产 | 伊人久久一区二区 | 国产激情一区二区三区 | 天天操 夜夜操 | 午夜电影在线播放 | 日韩国产在线 | 日韩成人av在线播放 | 成人亚洲精品久久久久软件 | 亚洲高清视频一区二区 | 欧美黄色性生活视频 | 精品一区二区三区在线观看 | 成人精品一区 | 视频三区 | 人人看人人草 | 精品国产一区二区在线 | 在线观看视频中文字幕 | 成人精品国产免费网站 | 成人在线免费看 | 免费艹逼视频 | 草草草网站 | 国产精品久久久久久妇女6080 | 日韩视频在线免费观看 | 日日干夜夜操天天操 | 在线视频中文字幕 | 男人的天堂中文字幕 | 欧美一区二区三区高清视频 | 午夜视频在线播放 | 天天影视网天天综合色在线播放 | 九九热这里只有精品在线观看 | 久久精品欧美一区二区三区不卡 | 日韩在线视频观看 | 日本特黄a级高清免费大片 成年人黄色小视频 | 三级成人在线 |