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

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

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

問題描述

我分析Java中的HashMap源代碼,得到一個關于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:

如果兩個對象根據equals(Object)方法相等,那么對兩個對象中的每一個調用hashCode方法必須產生相同的整數結果.

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))這樣的條件?

推薦答案

這只是一個優化:比較兩個整數比調用 equals() 更快.

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

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

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.

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

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

相關文檔推薦

Couchbase Bucket authentication error(Couchbase 存儲桶身份驗證錯誤)
admob 6.2.1 nullpointer exception(admob 6.2.1 空指針異常)
How to setup SDK in IntelliJ IDEA?(如何在 IntelliJ IDEA 中設置 SDK?)
My phone cannot be detected in eclipse to test run(eclipse 無法檢測到我的手機進行試運行)
platform-toolsaapt.exe directory missing in android SDK(android SDK 中缺少 platform-toolsaapt.exe 目錄)
error importing com.google.android.gms.*;(導入 com.google.android.gms.* 時出錯;)
主站蜘蛛池模板: 欧美精品在线一区 | 日本精品视频在线 | 天堂一区二区三区 | 99午夜 | 在线不卡视频 | 进去里视频在线观看 | 亚洲二区在线观看 | 国产日韩久久 | 人人草av | 国产成人免费观看 | av观看网站 | 精品久久一区二区三区 | 欧美在线观看视频 | 亚洲精品久 | 深夜福利视频网站 | 日韩国产在线 | 黄色福利 | 久久国产精品免费 | 亚洲福利影院 | 一区二区三区亚洲 | 亚洲天堂国产 | 久久免费影院 | 三级av片| 免费a在线观看 | 永久免费视频 | 人人看人人干 | 久久久一 | 99精品成人| 91极品视频 | 欧美成人综合 | 欧美一级特黄视频 | 国产1级片| 亚洲精品国产一区 | 国产天堂av | 69成人网 | 亚洲激情在线观看 | 亚洲成人久久久 | a级片在线免费观看 | www激情| 五月天综合网 | 91狠狠干|