• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Information about equals() anf hashcode() in java  RSS feed

 
Deepak Sahu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to know about equals() and hashcode() in java wth example with brief explaination.
 
dileep keely
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer to the below link, will fill some gap I guess.
http://www.coderanch.com/t/507916/java/java/Confusion-equals-method
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to learn gud tutorial dude...there is no ready made things...learn head first java book...it is great book for you.
 
venkatesh goud
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

this may help you..

equals() (javadoc) must define an equality relation (it must be reflexive, symmetric, and transitive). In addition, it must be consistent (if the objects are not modified, then it must keep returning the same value). Furthermore, o.equals(null) must always return false.

hashCode() (javadoc) must also be consistent (if the object is not modified in terms of equals(), it must keep returning the same value).

The relation between the two methods is:

Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().

for more

http://www.vipan.com/htdocs/hashcode_help.html

http://www.technofundo.com/tech/java/equalhash.html


 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That link has an error in; it uses == to compare Strings in a correct implementation (sic). It is also out of date, using J2SE1.4. Otherwise it is a good article Neither link tells us to use the @Override annotation, which one should use for any overridden method in Java5 or Java6.

This question about equals and hashCode comes up frequently; do a search and you find several good links: this post includes links to Odersky, Angelika Langer, and Bloch. Beware: the equals() method is by no means easy to override.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Sahu wrote:...I want to know about equals() and hashcode() in java...

The "hashcode contract" outlining the relationship between hashCode() and equals() is also in the API documentation under Object's hashCode method.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of good info: How to Write an Equality Method in Java
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search this forum; you will find many answers to similar questions here.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!