久久久久久久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.* 時出錯;)
主站蜘蛛池模板: 91精品国产综合久久久久久漫画 | 国产黄色大片网站 | 国产精品成人一区二区三区夜夜夜 | 欧美一区二区小视频 | 亚洲性视频 | 欧美一区不卡 | 亚洲精品欧美一区二区三区 | 欧美日韩亚洲国产 | 欧美一级大片免费看 | 美女黄视频网站 | 国产精品久久久久久久模特 | 国产免费一区二区 | 曰批视频在线观看 | 国产精品国色综合久久 | 日日草夜夜草 | 午夜精品在线观看 | 91一区二区三区 | 在线观看免费黄色片 | 一级毛片大全免费播放 | 精品成人免费视频 | 色就干| 日韩在线中文 | 麻豆视频在线免费观看 | 国产视频在线一区二区 | 午夜成人在线视频 | 一级黄色片毛片 | 国产一级成人 | 盗摄精品av一区二区三区 | 国产精品久久久久久模特 | 久久精品在线播放 | 在线免费黄色小视频 | 欧美一级片在线看 | 国产一区黄色 | 91精品国产一区二区三区蜜臀 | 伊人免费在线观看高清 | 国产精品久久久久久久久久 | 一级视频在线免费观看 | 91精品在线观看入口 | 欧美成人aaa级毛片在线视频 | 九九精品在线 | 精品久久国产 |