This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes purpose of hash code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "purpose of hash code" Watch "purpose of hash code" New topic

purpose of hash code

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Can someone plz tell what role hash code plays in object comparison? I am talking abt hashCode method of Object class.

Sean MacLean
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
This page might shed a bit of light on the subject.
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
In brief, the contract for equals(Object) and hashCode() requires that if a.equals(b), then a.hashCode() and b.hashCode() return the same value.
It is necessary to comply with this contract, even if not using the "hash" containers, because any class is allowed to rely on the contract. For example, suppose class Slow has an equals and hashCode method that are very slow. The contract for equals and hashCode() permits you to do something like this as an optimization:
It is sorta covered in the JavaRanch Style Guide.
subject: purpose of hash code
Similar Threads
doubt regarding hashcode
hashCode() from Object
Hash Code Implementation
equal() method of class Object