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

確保每個 Hashmap 桶/槽一個值

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

問題描述

有沒有辦法嚴格確保每個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?

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

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)實世界中并不常見,但我想提高我的理解.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 總是會根據(jù) key 的 hash code 計算出使用哪個桶.如果每個鍵具有相同的哈希碼,它們都將映射到同一個桶.如果不提供更好的 hashCode() 實現(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 實現(xiàn)(例如 Trove 的 THashMap).他們總是每個桶只有一個條目.但是性能不會提高,它們只是以不同的方式處理沖突,而且它們也無法解決您的根本問題:哈希碼錯誤.

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.

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

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

相關文檔推薦

Couchbase Bucket authentication error(Couchbase 存儲桶身份驗證錯誤)
How to setup SDK in IntelliJ IDEA?(如何在 IntelliJ IDEA 中設置 SDK?)
error importing com.google.android.gms.*;(導入 com.google.android.gms.* 時出錯;)
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 已準備好使用低于 2.2 的 SDK 目標?)
What is #39;savedInstanceState#39;?(什么是“已保存實例狀態(tài)?)
主站蜘蛛池模板: 国产精品99一区二区 | 午夜一级大片 | 亚洲 成人 av| 欧美一级二级在线观看 | 欧美一级片在线看 | 日韩在线不卡视频 | 精品精品 | 国产成人精品一区二区三区在线 | 午夜免费看视频 | 岛国av免费观看 | 免费毛片网站在线观看 | 欧美日韩在线高清 | 日韩喷潮| 午夜爽爽男女免费观看hd | 国产在线资源 | 成人高清视频在线观看 | 亚洲国产精品一区在线观看 | 精品欧美一区二区精品久久 | 九九国产在线观看 | 亚洲精品乱码久久久久久9色 | 91精品国产99 | 国产你懂的在线观看 | 天堂视频中文在线 | 亚洲综合大片69999 | 久久九九99 | 日韩精品一区二区三区四区视频 | 日日摸夜夜爽人人添av | 久久精品国产精品青草 | 日韩欧美视频在线 | 久久爱黑人激情av摘花 | 乱一性一乱一交一视频a∨ 色爱av | 羞羞免费网站 | 亚洲精品一区二区三区中文字幕 | 久久久久久久久久爱 | 日本亚洲一区 | 男女在线网站 | 久久精品 | 91久久 | 中文字幕一区在线观看视频 | 爽爽免费视频 | 人人射人人 |