首页 > 编程知识 正文

java hashmap 碰撞,Java - HashMap混淆了碰撞处理和get()方法

时间:2023-05-05 09:54:11 阅读:16183 作者:2037

I ' musingahashmapandihaven ' tbeenabletogetastraightansweronhowtheget () methodworksinthecaseofcollisions。

let ' s say n1 objectsgetplacedinthesamekey.aretheystoredinalinkedlist? aretheyoverwrittensothatonlythelastobjectplacedinthatkeyexiststhereanymore? aretheyusingsomeothercollisionmethod?

If they are placed in a LinkedList,isthereawaytoretrievethatentirelist? If not,istheresomeotherbuiltinmapforjavainwhichicandothis?

For my purposes,separate chaining would be ideal,as if there are collisions,ineedtobeabletolookthroughthelistandgetinformationationabonabouboution

Thanks for all your help!

解决方案

aretheyoverwrittensothatonlythelastobjectplacedinthatkeyexiststhereanymore?

Yes,assuming you ' reputtingmultiplevalueswiththesamekey (accordingtoobject.equals,not Object.hashCode.) that ' specod

ifthemappreviouslycontainedamappingforthekey,theoldvalueisreplacedbythespecifiedvalue。

ifyouwanttomapakeytomultiplevalues,you ' reprobablybetteroffusingsomethinglikeguava ' slist multimap, arraylistmultimapinspeecification whichmapskeystolistsofvalues.(disclosure : icontributetoguava.) if you can ' toleraterateate

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。