Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can two keys having different hashCode be a part of same bucket in HashMap in Java?

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.

like image 541
vijayinani Avatar asked May 08 '16 07:05

vijayinani


People also ask

Can different keys in a HashMap have the same hashCode?

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.

What will happen if two different objects have same hashCode in HashMap?

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.

Can two different keys have same hashCode?

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.

What happens if hashCode is different from same return?

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.


2 Answers

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.

like image 101
Eran Avatar answered Oct 03 '22 02:10

Eran


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.

like image 39
Idos Avatar answered Oct 03 '22 02:10

Idos