I have a HashMap. There are 16 buckets in it (by default). Now is it possible that two keys having different hashCodes be part of the same bucket? Or is it always a new bucket is created for a different hashCode and in this way the HashMap expands the bucket size?
Read many posts, but only confused myself.
It's perfectly legal for two unequal objects to have the same hash code. It's used by HashMap as a "first pass filter" so that the map can quickly find possible entries with the specified key. The keys with the same hash code are then tested for equality with the specified key.
Whenever two different objects have the same hash code, we call this a collision. A collision is nothing critical, it just means that there is more than one object in a single bucket, so a HashMap lookup has to look again to find the right object.
1) If two objects are equal (i.e. the equals() method returns true), they must have the same hashcode. 2) If the hashCode() method is called multiple times on the same object, it must return the same result every time. 3) Two different objects can have the same hash code.
Whenever it(hashcode) is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
Yes, it is possible. Since the number of buckets is much smaller than the number of possible hashCodes
(the number of buckets is proportional to the number of entries in the HashMap
while the number of possible hashCodes
is the number of possible int
values, which is much larger), the final mapping of a hashCode
to a bucket is done by some modulus operator, so multiple hashCodes
may be mapped to the same bucket (if, for example, you have 16 buckets, both the hashCodes
1 and 17 will be mapped to the same bucket (note that by hashCode
I don't mean the value returned by the hashCode
method, since HashMap
applies an additional function on that hashCode
in order to improve the distribution of the hash codes)).
That's why hashCode
alone is not enough to determine if the key we are looking for is present in the map - we have to use equals
as well.
Taken from How HashMap works in Java:
Since the internal array of HashMap is of fixed size, and if you keep storing objects, at some point of time hash function will return same bucket location for two different keys, this is called collision in HashMap. In this case, a linked list is formed at that bucket location and a new entry is stored as next node.
And then when there if we want to get
that object from the list we need equals()
:
If we try to retrieve an object from this linked list, we need an extra check to search correct value, this is done by equals() method. Since each node contains an entry, HashMap keeps comparing entry's key object with the passed key using equals() and when it return true, Map returns the corresponding value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With