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?
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:
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
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.
a.equals(b)
returns true
, bValue
will replace aValue
and bValue
will be returned.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 :
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.
In that case you could use IdentityHashMap, where different objects with same hash are considered as different based on their identities.