pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

當(dāng)對(duì)象 Hashcode 更改時(shí),Hashmap 或 Hashset 中的查找

What happens to the lookup in a Hashmap or Hashset when the objects Hashcode changes(當(dāng)對(duì)象 Hashcode 更改時(shí),Hashmap 或 Hashset 中的查找會(huì)發(fā)生什么)
本文介紹了當(dāng)對(duì)象 Hashcode 更改時(shí),Hashmap 或 Hashset 中的查找會(huì)發(fā)生什么的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

在 Hashmap 中,提供的鍵的哈希碼用于將值放置在哈希表中.在哈希集中,對(duì)象哈希碼用于將值放置在底層哈希表中.也就是說(shuō),hashmap 的優(yōu)點(diǎn)是你可以靈活地決定你想要什么作為 key,這樣你就可以做這樣的好事.

In a Hashmap the hash code of the key provided is used to place the value in the hashtable. In a Hashset the obects hashcode is used to place the value in the underlying hashtable. i.e the advantage of the hashmap is that you have the flexibility of deciding what you want as the key so you can do nice things like this.

Map<String,Player> players = new HashMap<String,Player>();

這可以將諸如玩家姓名之類的字符串映射到玩家本身.

This can map a string such as the players name to a player itself.

我的問(wèn)題是,當(dāng)鍵的 Hashcode 發(fā)生變化時(shí),查找會(huì)發(fā)生什么變化.

My question is is what happens to to the lookup when the key's Hashcode changes.

我希望這對(duì)于 Hashmap 來(lái)說(shuō)不是一個(gè)主要問(wèn)題,因?yàn)槲也幌M膊幌M荑€改變.在前面的例子中,如果球員的名字改變了,他就不再是那個(gè)球員了.但是,我可以使用鍵更改其他不是名稱的字段來(lái)查找玩家,并且將來(lái)的查找將起作用.

This i expect isn't such a major concern for a Hashmap as I wouldn't expect nor want the key to change. In the previous example if the players name changes he is no longer that player. However I can look a player up using the key change other fields that aren't the name and future lookups will work.

但是在 Hashset 中,因?yàn)槿绻腥松晕⒏膶?duì)象,則使用整個(gè)對(duì)象的哈希碼來(lái)放置項(xiàng)目,該對(duì)象的未來(lái)查找將不再解析到 Hashtable 中的相同位置,因?yàn)樗蕾囉谡麄€(gè)對(duì)象的哈希碼.這是否意味著一旦數(shù)據(jù)在 Hashset 中就不應(yīng)更改.還是需要重新散列?還是自動(dòng)完成等?這是怎么回事?

However in a Hashset since the entire object's hashcode is used to place the item if someone slightly changes an object future lookups of that object will no longer resolve to the same position in the Hashtable since it relies on the entire objects Hashcode. Does this mean that once data is in a Hashset it shouldnt be changed. Or does it need to be rehashed? or is it done automatically etc? What is going on?

推薦答案

在您的示例中,String 是不可變的,因此其哈希碼無(wú)法更改.但是假設(shè),如果對(duì)象的哈希碼在哈希表中作為鍵時(shí)確實(shí)發(fā)生了變化,那么就哈希表查找而言,它可能會(huì)消失.我在對(duì)相關(guān)問(wèn)題的回答中更詳細(xì)地介紹了:https://stackoverflow.com/a/13114376/139985.(最初的問(wèn)題是關(guān)于 HashSet,但 HashSet 實(shí)際上是一個(gè) HashMap,所以答案也涵蓋了這種情況.)

In your example, a String is immutable so its hashcode cannot change. But hypothetically, if the hashcode of an object did change while was a key in a hash table, then it would probably disappear as far as hashtable lookups were concerned. I went into more detail in this Answer to a related question: https://stackoverflow.com/a/13114376/139985 . (The original question is about a HashSet, but a HashSet is really a HashMap under the covers, so the answer covers this case too.)

可以肯定地說(shuō),如果 HashMap 或 TreeMap 的鍵發(fā)生變異,會(huì)影響它們各自的 hashcode()/equals(Object)compare(...)compareTo(...) 合約,則數(shù)據(jù)結(jié)構(gòu)將中斷".

It is safe to say that if the keys of either a HashMap or a TreeMap are mutated in a way that affects their respective hashcode() / equals(Object) or compare(...) or compareTo(...) contracts, then the data structure will "break".

這是否意味著一旦數(shù)據(jù)在 Hashset 中就不應(yīng)更改.

Does this mean that once data is in a Hashset it shouldn't be changed.

是的.

還是需要重新散列?還是自動(dòng)完成等?

Or does it need to be rehashed? or is it done automatically etc?

它不會(huì)自動(dòng)重新散列.HashMap 不會(huì)注意到鍵的哈希碼已更改.事實(shí)上,當(dāng) HashMap 調(diào)整大小時(shí),您甚至不會(huì)重新計(jì)算哈希碼.數(shù)據(jù)結(jié)構(gòu)記住原始哈希碼值,以避免在哈希表調(diào)整大小時(shí)重新計(jì)算所有哈希碼.

It won't be automatically rehashed. The HashMap won't notice that the hashcode of a key has changed. Indeed, you won't even get recomputation of the hashcode when the HashMap resizes. The data structure remembers the original hashcode value to avoid having to recalculate all of the hashcodes when the hash table resizes.

如果您知道某個(gè)鍵的哈希碼將要更改,則需要在更改該鍵之前從表中刪除該條目,然后再將其添加回來(lái).(如果你在改變密鑰后嘗試 remove/put 它,remove 可能會(huì)找不到條目.)

If you know that the hashcode of a key is going to change you need to remove the entry from the table BEFORE you mutate the key, and add it back afterwards. (If you try to remove / put it after mutating the key, the chances are that the remove will fail to find the entry.)

發(fā)生了什么事?

發(fā)生的事情是您違反了合同.不要那樣做!

What is going on is that you violated the contract. Don't do that!

合同由兩部分組成:

  1. javadoc for Object.

當(dāng)對(duì)象的哈希碼是哈希表中的鍵時(shí),它不能更改的附加約束.

An additional constraint that an object's hashcode must not change while it is a key in a hash table.

HashMap javadoc,但 javadoc for Map 說(shuō):

The latter constraint is not stated specifically in the HashMap javadoc, but the javadoc for Map says this:

注意:如果將可變對(duì)象用作映射鍵,則必須非常小心.如果對(duì)象的值以影響 equals 比較的方式更改,而對(duì)象是映射中的鍵,則不會(huì)指定映射的行為.

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.

影響相等性的更改(通常)也會(huì)影響哈希碼.在實(shí)現(xiàn)級(jí)別,如果 HashMap 條目的鍵的哈希碼發(fā)生更改,則該條目通常現(xiàn)在位于錯(cuò)誤的哈希桶中,并且對(duì) HashMap 執(zhí)行查找的方法.

A change that affects equality (typically) also affects the hashcode. At the implementation level, if a HashMap entry's key's hashcode changes, the entry will typically now be in the wrong hash bucket and will be invisible to HashMap methods that perform lookups.

這篇關(guān)于當(dāng)對(duì)象 Hashcode 更改時(shí),Hashmap 或 Hashset 中的查找會(huì)發(fā)生什么的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉(zhuǎn)換為 Map)
Getting data from JSON(從 JSON 獲取數(shù)據(jù))
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對(duì)象列表轉(zhuǎn)換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個(gè)固定的Key對(duì)應(yīng)一個(gè)HashSet創(chuàng)建一個(gè)HashMap.出發(fā)點(diǎn))
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無(wú)法寫(xiě)入請(qǐng)求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 螺旋压榨机-刮泥机-潜水搅拌机-电动泥斗-潜水推流器-南京格林兰环保设备有限公司 | 除尘器布袋骨架,除尘器滤袋,除尘器骨架,电磁脉冲阀膜片,卸灰阀,螺旋输送机-泊头市天润环保机械设备有限公司 | 电磁辐射仪-电磁辐射检测仪-pm2.5检测仪-多功能射线检测仪-上海何亦仪器仪表有限公司 | 不锈钢列管式冷凝器,换热器厂家-无锡飞尔诺环境工程有限公司 | 利浦顿蒸汽发生器厂家-电蒸汽发生器/燃气蒸汽发生器_湖北利浦顿热能科技有限公司官网 | 剪刃_纵剪机刀片_分条机刀片-南京雷德机械有限公司 | 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 沈阳缠绕包装机厂家直销-沈阳海鹞托盘缠绕包装机价格 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 智能案卷柜_卷宗柜_钥匙柜_文件流转柜_装备柜_浙江福源智能科技有限公司 | 商标转让-购买商标专业|放心的商标交易网-蜀易标商标网 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 专业音响设备_舞台音响设备_会议音响工程-首选深圳一禾科技 | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 塑料异型材_PVC异型材_封边条生产厂家_PC灯罩_防撞扶手_医院扶手价格_东莞市怡美塑胶制品有限公司 | 空气能采暖,热泵烘干机,空气源热水机组|设备|厂家,东莞高温热泵_正旭新能源 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 口信网(kousing.com) - 行业资讯_行业展会_行业培训_行业资料 | 上海璟文空运首页_一级航空货运代理公司_机场快递当日达 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 | 山东石英砂过滤器,除氟过滤器「价格低」-淄博胜达水处理 | 手板-手板模型-手板厂-手板加工-生产厂家,[东莞创域模型] | 油漆辅料厂家_阴阳脚线_艺术漆厂家_内外墙涂料施工_乳胶漆专用防霉腻子粉_轻质粉刷石膏-魔法涂涂 | 泰国试管婴儿_泰国第三代试管婴儿费用|成功率|医院—新生代海外医疗 | 深圳3D打印服务-3D打印加工-手板模型加工厂-悟空打印坊 | 玻璃钢型材-玻璃钢风管-玻璃钢管道,生产厂家-[江苏欧升玻璃钢制造有限公司] | 机构创新组合设计实验台_液压实验台_气动实训台-戴育教仪厂 | 排烟防火阀-消防排烟风机-正压送风口-厂家-价格-哪家好-德州鑫港旺通风设备有限公司 | 铁盒_铁罐_马口铁盒_马口铁罐_铁盒生产厂家-广州博新制罐 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 国际船舶网 - 船厂、船舶、造船、船舶设备、航运及海洋工程等相关行业综合信息平台 | 混合气体腐蚀试验箱_盐雾/硫化氢/气体腐蚀试验箱厂家-北京中科博达 |