简体   繁体   English

如何在哈希冲突后检索值

[英]How to retrieve values after a hash collision

I have read in many places that after a hash collision in Java it is internally using a linked list/tree, based on the number of hash collisions.Till this is fine,我在很多地方都读到过,在 Java 中发生哈希冲突后,它会根据哈希冲突的数量在内部使用链表/树。直到这很好,

But how to retrieve back the expected value using the 'key'但是如何使用“键”取回预期值

It just iterates the linked list stored in that bucket and checks the elements using equals which has no collisions .它只是迭代存储在该存储桶中的链表,并使用没有碰撞的equals检查元素。

The running time for that is linear, but only linear in the amount of items stored in that specific bucket, so it is okay as long as the buckets are kept balanaced well enough.其运行时间是线性的,但仅与存储在该特定存储桶中的项目数量呈线性关系,因此只要存储桶保持足够的平衡就可以了。

Look at this illustration ( source ):看看这个插图( 来源):

在此处输入图片说明

So the implementation will make sure that a get operation, even if it has collisions, gives back the correct result in the end.因此,实现将确保get操作,即使它有冲突,最终返回正确的结果。


Note that Javas HashSet and HashMap are not a pure hashtable like illustrated.请注意,Javas HashSetHashMap不是如图所示的纯哈希表。 They will switch to a red-black tree internally after a certain threshold.在达到一定的阈值后,它们会在内部切换到红黑树

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM