久久久久久久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ā)生,或者至少?gòu)慕Y(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)?)
主站蜘蛛池模板: 日本又色又爽又黄又高潮 | 久久最新精品 | 日本精品一区二区三区在线观看视频 | 欧美一级片久久 | 中文字幕在线观看视频一区 | 99精品国产一区二区青青牛奶 | 日韩精品一区二区三区 | 亚洲综合色视频在线观看 | 日本福利在线 | 视频一区二区中文字幕日韩 | 成人精品福利 | 中文字幕一区二区三区精彩视频 | 自拍偷拍av| 成人毛片一区二区三区 | 成年人在线视频 | 国产一级片精品 | 九九热这里只有精品6 | 日韩欧美综合在线视频 | 伊人天堂网 | 久久精品一区二区三区四区 | 亚洲一区中文字幕 | 欧美一级毛片久久99精品蜜桃 | 亚洲日本一区二区 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 国产精品久久久久久久久久久免费看 | 中文字幕一区二区三区在线乱码 | 伊人成人免费视频 | 久久久69| 99久久99 | 91精品国产91久久久久游泳池 | 天天草天天干天天 | 欧美激情精品久久久久 | 成人福利视频网站 | 日韩在线视频免费观看 | 国产高清精品一区 | 久久国产精品一区二区三区 | 日韩综合在线视频 | 91精品国产综合久久福利软件 | 国产精品国产三级国产aⅴ中文 | 美国av毛片| 婷婷久久一区 |