简体   繁体   中英

Hashing function and keys

While going through Kathy Sierra's book I stumbled across this code fragment:

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? 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.

The put() method starts by calling hashCode() on your key. 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.

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.

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.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (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.)

See the javadoc at java.lang.Object 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

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() Example

More information of HashMap get() retrieval of values. Here

When a HashMap is used, the keys in it are unique. 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.

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.

Hence, once the equals() test passes, the object won't be inserted into the map since the map should have unique keys. 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.

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.

So, in the above case, equals() return false as the strings are not equal but the hashCode will be same. So the two objects will be placed in the same hash code bucket.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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