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

確保每個(gè) Hashmap 桶/槽一個(gè)值

Ensuring One Value per Hashmap bucket/slot(確保每個(gè) Hashmap 桶/槽一個(gè)值)
本文介紹了確保每個(gè) Hashmap 桶/槽一個(gè)值的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

有沒有辦法嚴(yán)格確保每個(gè)Hashmap桶的條目數(shù)不篡改Java中的object.hashcode()函數(shù)?

Is there a way to strictly ensure the number of entries per Hashmap bucket without tampering the the object.hashcode() function in Java?

負(fù)載因子是一個(gè)平均值:(條目數(shù))/(桶數(shù)).本質(zhì)上,假設(shè)我有一個(gè)容量為 1000 的 Hashmap.為了這個(gè)示例,假設(shè)我使用 1 的負(fù)載因子.我將要存儲(chǔ)在 HashMap 中的 100 個(gè)對(duì)象具有錯(cuò)誤的哈希碼函數(shù),它總是返回每個(gè)對(duì)象的值相同.當(dāng)我存儲(chǔ)完 100 個(gè)對(duì)象后,它們都將映射到同一個(gè) HashMap 存儲(chǔ)桶,我最終會(huì)獲得 LinkedList 的性能.負(fù)載因子將保持沉默,因?yàn)?100 個(gè)條目/1000 個(gè)桶 = 0.1 <1. 現(xiàn)在如果我放置 1 M 個(gè)相同的對(duì)象會(huì)發(fā)生什么.因?yàn)橛肋h(yuǎn)不會(huì)觸發(fā) LF,所以永遠(yuǎn)不會(huì)調(diào)整 HashMap 的大小(無論如何都不會(huì)使用).

The Load Factor is an average: (# of entries) / (# of buckets). In essence, let's say I have a Hashmap of capacity 1000. For the sake of this example, say I use a Load Factor of 1. The 100 objects I'm going to be storing in the HashMap have bad hashcode function which always return the same value for every object. When I'm done storing 100 objects, they will all map of the same HashMap bucket and I eventually end up with LinkedList performance. The Load Factor will sit silent because 100 entries / 1000 buckets = 0.1 < 1. Now what happens if I put 1 M of the same objects. The HashMap will never be resized (no use anyways) as the LF will never be triggered.

我知道這在現(xiàn)實(shí)世界中并不常見,但我想提高我的理解.HashMap 有沒有辦法防止這種情況發(fā)生,或者至少從結(jié)構(gòu)本身得到一些警告?

I know this is an uncommon scenario in real world but would like to improve my understanding. Is there a way in HashMap to prevent this or at least get some warning from the structure itself?

推薦答案

HashMap 總是會(huì)根據(jù) key 的 hash code 計(jì)算出使用哪個(gè)桶.如果每個(gè)鍵具有相同的哈希碼,它們都將映射到同一個(gè)桶.如果不提供更好的 hashCode() 實(shí)現(xiàn),您將無法阻止您描述的行為.

A HashMap will always calculate which bucket to use based on the key's hash code. If each key has the same hash code, they will all map to the same bucket. You cannot prevent the behavior you described without providing a better hashCode() implementation.

您可以查看使用開放尋址的 Map 實(shí)現(xiàn)(例如 Trove 的 THashMap).他們總是每個(gè)桶只有一個(gè)條目.但是性能不會(huì)提高,它們只是以不同的方式處理沖突,而且它們也無法解決您的根本問題:哈希碼錯(cuò)誤.

You could look at Map implementations that use open addressing (e.g. Trove's THashMap). They will always have just one entry per bucket. But the performance will not improve, they just deal with collisions in a different way, and they also won't solve your root problem : a bad hash code.

這篇關(guān)于確保每個(gè) Hashmap 桶/槽一個(gè)值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Couchbase Bucket authentication error(Couchbase 存儲(chǔ)桶身份驗(yàn)證錯(cuò)誤)
How to setup SDK in IntelliJ IDEA?(如何在 IntelliJ IDEA 中設(shè)置 SDK?)
error importing com.google.android.gms.*;(導(dǎo)入 com.google.android.gms.* 時(shí)出錯(cuò);)
Cannot list image publishers from Azure java SDK(無法從 Azure java SDK 列出圖像發(fā)布者)
How do I know that the Soundpool is ready using SDK target below 2.2?(我如何知道 Soundpool 已準(zhǔn)備好使用低于 2.2 的 SDK 目標(biāo)?)
What is #39;savedInstanceState#39;?(什么是“已保存實(shí)例狀態(tài)?)
主站蜘蛛池模板: 亚洲午夜在线 | 国产成人精品免费视频 | 四虎在线播放 | 伊人春色av | 夜夜操夜夜 | 热久久中文字幕 | 在线看91| 高清一级片 | 欧美一区二区在线 | 成人免费在线视频观看 | 亚洲视频在线免费观看 | 成人观看视频 | 97精品在线观看 | 国产麻豆91| 午夜精品视频 | 成人国产网站 | 国产精品一二三四区 | 久久婷婷网 | 国产盗摄一区二区 | 亚洲一区二区在线播放 | 欧美日韩亚洲视频 | 日本欧美在线 | 国产香蕉视频在线观看 | av免费网| 91中文在线 | 久久国产精品一区二区三区 | 国产超碰人人模人人爽人人添 | 色呦呦国产精品 | 免费一区二区视频 | 少妇搡bbbb搡bbb搡毛茸茸 | 国产欧美日本 | 国产精品久久久久久久久 | 午夜网址 | 日韩黄网 | 91久久精品日日躁夜夜躁欧美 | 97在线免费观看 | 国产日韩欧美综合 | av高清不卡 | 亚洲精品播放 | 91蜜桃视频| 中文字幕第一区综合 |