This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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


JavaRanch » Java Forums » Java » Java in General
Bookmark "purpose of hash code" Watch "purpose of hash code" New topic
Author

purpose of hash code

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

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


Groovy
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
This page might shed a bit of light on the subject.
http://java.sun.com/docs/books/tutorial/java/javaOO/objectclass.html
Sean
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:
 
Have you checked out Aspose?
 
subject: purpose of hash code
 
It's not a secret anymore!