Normally when you have an object an objects Equals methods is equal to another object of same type if the two hashcodes are alike.
Does Integer work the same way by comparing hashcodes or does it work in a different way?
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.
Two objects that have the same hashcode need not be
equals().Two objects that are
equals()must have the same hashcode.This basically means you may have (occasional) hashcode collisions.
The
Integerequals()method simply compares theintvalue it wraps with the otherInteger‘sintvalue. Hashcodes don’t come into it.Note that the
equals()andhashcodeaggrement is not strictly required, but it is relied upon by the numerous JDK classes that use an internalHashtable, eg the variousjava.util.Hashxxxclasses.