top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it possible for two unequal objects to have the same hashcode in Java?

+2 votes
942 views
Is it possible for two unequal objects to have the same hashcode in Java?
posted Oct 12, 2016 by Sahana

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

It is not required that if two objects are unequal according to the equal(), then calling the hashcode method on each of the two objects must produce distinct values.
Depending on the hashing function, 2 different objects can have the same hash code. However, 2 objects wich are the same must produce the same result when hashed (unless someone implemented a hashing function with random numbers in which case it's useless)

For example, if I am hashing integers and my hashing function is simply (n % 10) then the number 17 and the number 27 will produce the same result. This does not mean that those numbers are the same.

answer Nov 7, 2016 by Karthick.c
...