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屋-程序員
主站蜘蛛池模板: 阳光1号桔柚_无核沃柑_柑橘新品种枝条苗木批发 - 苧金网 | 酒精检测棒,数显温湿度计,酒安酒精测试仪,酒精检测仪,呼气式酒精检测仪-郑州欧诺仪器有限公司 | 南京欧陆电气股份有限公司-风力发电机官网| 新疆系统集成_新疆系统集成公司_系统集成项目-新疆利成科技 | 精密模具加工制造 - 富东懿| 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 苏州防水公司_厂房屋面外墙防水_地下室卫生间防水堵漏-苏州伊诺尔防水工程有限公司 | 运动木地板厂家,篮球场木地板品牌,体育场馆木地板安装 - 欧氏运动地板 | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | Duoguan 夺冠集团| LOGO设计_品牌设计_VI设计 - 特创易| 湖南专升本-湖南省专升本报名-湖南统招专升本考试网 | 工业铝型材生产厂家_铝合金型材配件批发精加工定制厂商 - 上海岐易铝业 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 工业铝型材生产厂家_铝合金型材配件批发精加工定制厂商 - 上海岐易铝业 | 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | 球盟会·(中国)官方网站 | 304不锈钢无缝管_不锈钢管厂家 - 隆达钢业集团有限公司 | 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 超声波气象站_防爆气象站_空气质量监测站_负氧离子检测仪-风途物联网 | 武汉创亿电气设备有限公司_电力检测设备生产厂家 | 阿里巴巴诚信通温州、台州、宁波、嘉兴授权渠道商-浙江联欣科技提供阿里会员办理 | 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 多功能干燥机,过滤洗涤干燥三合一设备-无锡市张华医药设备有限公司 | 土壤水分自动监测站-SM150便携式土壤水分仪-铭奥仪器 | led太阳能路灯厂家价格_风光互补庭院灯_农村市政工程路灯-中山华可路灯品牌 | 工业rfid读写器_RFID工业读写器_工业rfid设备厂商-ANDEAWELL | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 美名宝起名网-在线宝宝、公司、起名平台 | 爱科技iMobile-专业的科技资讯信息分享网站| sfp光模块,高速万兆光模块工厂-性价比更高的光纤模块制造商-武汉恒泰通 | 北京普辉律师事务所官网_北京律师24小时免费咨询|法律咨询 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 岩棉切条机厂家_玻璃棉裁条机_水泥基保温板设备-廊坊鹏恒机械 | 烟台游艇培训,威海游艇培训-烟台市邮轮游艇行业协会 | 宝元数控系统|对刀仪厂家|东莞机器人控制系统|东莞安川伺服-【鑫天驰智能科技】 | 冷却塔降噪隔音_冷却塔噪声治理_冷却塔噪音处理厂家-广东康明冷却塔降噪厂家 | 展厅设计公司,展厅公司,展厅设计,展厅施工,展厅装修,企业展厅,展馆设计公司-深圳广州展厅设计公司 | 原子吸收设备-国产分光光度计-光谱分光光度计-上海光谱仪器有限公司 |