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

具有不同哈希值的鍵是否也映射到 HashMap 中的相

Do keys with different hashes also get mapped to the same index in HashMap?(具有不同哈希值的鍵是否也映射到 HashMap 中的相同索引?)
本文介紹了具有不同哈希值的鍵是否也映射到 HashMap 中的相同索引?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

查看代碼,特別是第 393 行,看起來不同的哈希值已映射到同一個索引.我的理解是哈希碼用于確定要使用 HashMap 中的哪個桶,并且桶由具有相同哈希碼的所有條目的鏈表組成.他們為什么要檢查 e.hash == hash ?

Looking at the code specifically line 393, it looks like different hashes have been mapped to same index. I had an understanding that the hashcode is used to determine what bucket in a HashMap is to be used, and the bucket is made up of a linked list of all the entries with the same hashcode. They why have the e.hash == hash check ?



    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 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;
    }

推薦答案

由于 hashcode 可以是 2^32 值中的一個,hashmap 很少有這么多桶(僅表需要 16GB 內存).所以是的,您可以在地圖的相同存儲桶中擁有具有不同哈希值的對象(AFAIK 它是 hachCode % numberOfBuckets 的簡單模運算).

Since a hashcode can be one in 2^32 values, it is rare that the hashmap has so many buckets (just the table would require 16GB of memory). So yes, you can have objects with different hashes in the same buckets of the maps (AFAIK it is a simple modulus operation of hachCode % numberOfBuckets).

注意代碼不是直接使用key.hashCode(),而是hash(key.hashCode()).

Note that the code does not use directly key.hashCode(), but hash(key.hashCode()).

這篇關于具有不同哈希值的鍵是否也映射到 HashMap 中的相同索引?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: www.精品一区 | 伊人网99| 精品日韩在线 | 九九色综合 | 在线观看视频亚洲 | 久久综合婷婷 | 国产91在线 | 亚洲 | 欧美成人精品在线 | 久久大陆| 久久国产精品偷 | av电影一区二区 | 国内精品久久影院 | 欧美 日韩 中文 | 淫片专区| 久久综合狠狠综合久久综合88 | 久久精品视频亚洲 | 精品免费 | 亚洲一区二区视频在线播放 | 91视频在线看| 在线观看亚洲专区 | 中国美女一级黄色片 | 日韩成人影院 | 亚洲精品乱码久久久久v最新版 | 国产欧美精品一区二区三区 | 暴草美女 | 欧区一欧区二欧区三免费 | 欧美日韩在线观看视频 | 国产超碰人人爽人人做人人爱 | 亚洲一区二区三区在线免费观看 | 蜜桃一区二区三区 | 日韩精品成人免费观看视频 | 久草在线在线精品观看 | 午夜精品在线 | 久久久女 | 免费精品久久久久久中文字幕 | 日本欧美视频 | 亚洲狠狠| 国产成人精品视频 | 国产一二区视频 | 狠狠艹 | 欧美日韩国产精品一区 |