Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

purpose of hash code

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can someone plz tell what role hash code plays in object comparison? I am talking abt hashCode method of Object class.
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic