How do you come up with a hash function for a generic object? There is the constraint that two objects need to have the same hash value if they are “equal” as defined by the user. How does Java accomplish this?
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.
Hashing of an object is established by overriding
hashCode()method, which the developer can override.Java uses prime numbers in the default hashcode calculation.
If the
equals()andhashCode()method aren’t implemented, the JVM will generate hashcode implicitly for the object (for Serializable classes, aserialVersionUIDis generated).