简体   繁体   中英

What happens if two different objects have the same hashcode?

It is my understanding that two unequal objects can have the same hashcode. How would this be handled when adding or retrieving from a HashMap java?

They will just be added to the same bucket and equals() will be used to distinguish them. Each bucket can contain a list of objects with the same hash code.

In theory you can return the same integer as a hash code for any object of given class, but that would mean that you loose all performance benefits of the hash map and, in effect, will store objects in a list.

In HashMap, keys along with their associative values are stored in a linked list node in the bucket and the keys are essentially compared in hashmap using equals() method not by hashcode.

hm.put("a","aValue"); // Suppose hashcode created for key "a" is 209 
hm.put("b","bValue"); // Here hashcode created for key "b" is 209 as well.
  • If a.equals(b) returns true , bValue will replace aValue and bValue will be returned.
  • If a.equals(b) returns false , another node will be created in the bucket list, so when you call get("b") you will get bValue since a.equals(b) is false .

HashMap is working on the concept of hashing and indexing. Internally HashMap stores values in Array of Nodes. Each node behaves as LinkedList.

Each node of linked list have 4 values :

  1. int hash
  2. K key
  3. V value
  4. Node<K, V> next

HashMap Internal structure:

HashMap内部结构图像

While inserting the value in HashMap, first hashcode of Key is generated and based on some Algorithm it will calculate the index.

So our value will store in specific index with hashcode, key, value and address of next element.

While retrieving the value from HashMap, first hashcode will generate and then index(same way as at the time of insertion). While getting the value from index, first it will check for hashcode, if hashcode will match then only it will check for key from Node by using equals method. If key will match then only it will return the value or else it will check for next Node with same hashcode.

在这种情况下,您可以使用IdentityHashMap,其中具有相同散列的不同对象基于其身份被视为不同。

When two unequal objects have the same hash value, this causes a collision in the hash table, because both objects want to be in the same slot (sometimes called a bucket). The hash algorithm must resolve such collisions. Reaching back into fading memories of my college algorithms course, I remember three basic ways of doing this:

  1. Look for the next empty slot in the hash table and put the object there. Pros: easy to implement, cons: can lead to clustering of objects and degrade performance, capacity may be exceeded
  2. Have a secondary hash function to use when there's a conflict: Pros: usually fast, cons: must write a second hash function and may still get collisions, and capacity may be exceeded
  3. Make a linked-list of objects from the conflicted slot of the hash table. Pros/cons: usually fast for decent hash function and load factors, but can degrade to linear search in worst case

I think the Java hash classes use the third method, but they might use a combination approach. The key to good hashing though is to make sure the hash table has a big enough capacity and to write good hash functions. A hash table that only has as many buckets as objects it is holding will probably have conflicts. Usually you want the hash table to be about twice as big as the number of objects it stores. Java's HashMap will grow as needed, but you can give it a starting capacity and load factor if you want.

The hash function is up to the programmer. You could just return 0 for all objects, but that will mean the hashing (both storage and retrieval) will become O(n) instead of O(1) ... or in lay terms, it will be dog slow.

Reference : http://www.coderanch.com/t/540275/java/java/objects-hashcode-HashMap-retrieve-objects

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