问题
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?
回答1:
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.
回答2:
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)
returnstrue
,bValue
will replaceaValue
andbValue
will be returned. - If
a.equals(b)
returnsfalse
, another node will be created in the bucket list, so when you callget("b")
you will getbValue
sincea.equals(b)
isfalse
.
回答3:
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 :
int hash
K key
V value
Node<K, V> next
HashMap Internal structure:
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.
回答4:
In that case you could use IdentityHashMap, where different objects with same hash are considered as different based on their identities.
回答5:
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:
- 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
- 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
- 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
来源:https://stackoverflow.com/questions/11195027/what-happens-if-two-different-objects-have-the-same-hashcode