jQuery in Action, 3rd edition
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
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: 8927

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:
I agree. Here's the link: http://aspose.com/file-tools
subject: purpose of hash code
It's not a secret anymore!