Why did java designers impose a mandate that
if obj1.equals(obj2) then
obj1.hashCode() MUST Be == obj2.hashCode()
Why did java designers impose a mandate that if obj1.equals(obj2) then obj1.hashCode() MUST Be
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As far as I know that’s not baked into the language – you could technically have objects whose equals() method does not check the hashcode but you’ll get pretty peculiar results.
In particular if you put a bunch of these objects into a HashMap or HashSet the map/set will use the hashCode() method to determine whether the objects may be duplicates – so you can have a situation where a collection will store 2 objects you’ve defined as equals (which should never happen) because they’re each returning different hashCodes.