简体   繁体   English

使用 Bucket 方法的 Java HashTable

[英]Java HashTable using Bucket Method

How do I search a certain bucket in a hashing solution to find a key?如何在散列解决方案中搜索某个桶以找到密钥? I am having trouble figuring out how to see if my key is already in a given bucket number.我无法弄清楚如何查看我的密钥是否已经在给定的存储桶编号中。 I don't understand how to read buckets in an array.我不明白如何读取数组中的桶。

I am writing my own Hash data structure using buckets not Java's.我正在使用桶而不是 Java 来编写我自己的哈希数据结构。

Once you have found the bucket where the item should be, based on hashcode, you will then have to look for the item in question among all the objects in the same bucket.一旦您根据哈希码找到了该项目所在的存储桶,您就必须在同一个存储桶中的所有对象中查找有问题的项目。 Now all of these objects have the same hashCode, because they are all in the same bucket.现在所有这些对象都有相同的 hashCode,因为它们都在同一个桶中。 So you will have to actually compare these objects with the .equals method to see if the item you are actually looking for is there.因此,您必须实际将这些对象与 .equals 方法进行比较,以查看您实际要查找的项目是否存在。

How you manage this group of items that all share the same bucket is up to you.您如何管理这组共享同一个存储桶的项目取决于您。 You might have a list, or a sub array, or any data structure that holds a collection of objects.您可能有一个列表、一个子数组或任何包含对象集合的数据结构。

In fact, you don't necessarily need to hold them all in the same bucket at all.事实上,您根本不需要将它们全部放在同一个存储桶中。 There are schemes called open hashing where items with the same hash 'spill' out of the target bucket, and occupy successive buckets in the top array.有一种称为开放散列的方案,其中具有相同散列的项目从目标桶中“溢出”,并占用顶部数组中的连续桶。

Without knowing your exact data structure I can't be more specific.在不知道您的确切数据结构的情况下,我无法更具体。 But basically you use hashCode to get you to the top bucket, then you use equals to find the object within the group of objects that have the same hashcode.但基本上,您使用 hashCode 将您带到顶部存储区,然后使用 equals 在具有相同哈希码的对象组中查找对象。

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

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