简体   繁体   English

散列函数和键

[英]Hashing function and keys

While going through Kathy Sierra's book I stumbled across this code fragment: 在浏览Kathy Sierra的书时,我偶然发现了这段代码:

m.put("k1", new Dog("aiko"));   // add some key/value pairs
m.put("k2", Pets.DOG);
m.put(Pets.CAT, "CAT key");
Dog d1 = new Dog("clover");
m.put(d1, "Dog key");
m.put(new Cat(), "Cat key");

Maps are used to store stuff in the keys and values format. 地图用于以密钥和值格式存储内容。 Would someone tell me what is actually stored in key when we enter "k1" or new Cat() as a key? 当我们输入“k1”或新的Cat()作为键时,有人会告诉我实际存储在键中的内容吗? Are references to these objects are stored or the value of hashcode? 是存储对这些对象的引用还是哈希码的值? I am totally confused with this. 我完全对此感到困惑。 Please advice. 请指教。

And it would be appreciated if you could point me towards further reading material. 如果你能指出我进一步阅读材料,将不胜感激。

The map is an array of N buckets. 地图是N个桶的数组。

The put() method starts by calling hashCode() on your key. put()方法首先在键上调用hashCode() From this hash code, it uses a modulo to get the index of the bucket in the map. 从这个哈希码中,它使用模数来获取地图中存储桶的索引。

Then, it iterates through the entries stored in the linked list associated with the found bucket, and compares each entry key with your key, using the equals() method. 然后,它遍历存储在与找到的存储桶关联的链接列表中的条目,并使用equals()方法将每个条目键与您的密钥进行比较。

If one entry has a key equal to your key, its value is replaced by the new value. 如果一个条目的密钥等于您的密钥,则其值将替换为新值。 Else, a new entry is created with the new key and the new value, and stored in the linked list associated with the bucket. 否则,使用新密钥和新值创建新条目,并将其存储在与存储桶关联的链接列表中。

Since Cat instances and String instances are never equal, a value associated with a String key will never be modified by putting a value associated with a Cat key. 由于Cat实例和String实例永远不会相等,因此永远不会通过放置与Cat键关联的值来修改与String键关联的值。

It will be defined by your object. 它将由您的对象定义。

You have to create a hashCode() and a equals() method so it can be stored in your hashtable. 您必须创建一个hashCode()和一个equals()方法,以便它可以存储在您的哈希表中。

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. 尽可能合理,Object类定义的hashCode方法确实为不同的对象返回不同的整数。 (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.) (这通常通过将对象的内部地址转换为整数来实现,但JavaTM编程语言不需要此实现技术。)

See the javadoc at java.lang.Object http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#hashCode () 请参阅java.lang.Object中的javadoc http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#hashCode ()

or you can read this for an explanation http://www.javaworld.com/javaworld/javaqa/2002-06/01-qa-0621-hashtable.html 或者你可以阅读这个解释http://www.javaworld.com/javaworld/javaqa/2002-06/01-qa-0621-hashtable.html

I hope it helps 我希望它有所帮助

Storing the value to HashMap depends on the hashcode() and equals() method.Please find the more reference from here. 将值存储到HashMap取决于hashcode()equals() 。请从此处找到更多引用。

HashMap - hashcode() Example HashMap - hashcode()示例

More information of HashMap get() retrieval of values. 有关HashMap get()检索值的更多信息。 Here 这里

When a HashMap is used, the keys in it are unique. 使用HashMap时,其中的键是唯一的。 This uniqueness of the keys is checked in Java from the definition of the equals() and hashCode() methods that the class of the objects under consideration provides. 在Java中根据所考虑对象的类提供的equals()和hashCode()方法的定义来检查密钥的唯一性。

This is done by comparing using the equals() method first and if it returns equal then comparing using hashCode().Also, you must be knowing that each reference pointing to an object has a bit pattern which may be different for multiple references referring to the same object. 这是通过首先使用equals()方法进行比较并且如果它返回相等然后使用hashCode()进行比较来完成的。此外,您必须知道指向对象的每个引用都有一个位模式,对于多个引用可能不同同一个对象。

Hence, once the equals() test passes, the object won't be inserted into the map since the map should have unique keys. 因此,一旦equals()测试通过,对象将不会插入到地图中,因为地图应该具有唯一键。 So, each hashCode value for objects which are keys in the map will form different buckets for a range of hashCode values and the object will be grouped accordingly. 因此,作为映射中的键的对象的每个hashCode值将为一系列hashCode值形成不同的桶,并且对象将相应地分组。

EDIT to provide an example: 编辑提供一个例子:

For example, let us consider that two objects have a String attribute with values "hello" and "hlleo" and suppose the hashCode() function is programmed such that the hash code of an object is the sum of the ASCII values of the characters in the String attribute and the equals() method returns true if the values of the String attribute are equal. 例如,让我们考虑两个对象具有值为“hello”和“hlleo”的String属性,并假设hashCode()函数被编程为使得对象的哈希码是字符的ASCII值的总和。如果String属性的值相等,则String属性和equals()方法返回true。

So, in the above case, equals() return false as the strings are not equal but the hashCode will be same. 因此,在上面的情况中,equals()返回false,因为字符串不相等但hashCode将是相同的。 So the two objects will be placed in the same hash code bucket. 因此,这两个对象将被放置在相同的哈希码桶中。

Hope that helps. 希望有所帮助。

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

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