Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals and hashcode methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals and hashcode methods" Watch "equals and hashcode methods" New topic
Author

equals and hashcode methods

Murali Kakarla
Ranch Hand

Joined: Jul 11, 2007
Posts: 80
I have written a class Person and overridden equals and hashcode methods. But while adding Person objects to HashSet I am able to add duplicates. So it seems my implementation of equals/hashcode is not correct.

Can someone please tell me where I am doing wrong?



At line 1 I am getting HashSet size as 4 instead of 3!!!


Thanks
Murali...
[ August 07, 2007: Message edited by: Murali Kakarla ]
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Try changing hashcode to hashCode!


Mani
Quaerendo Invenietis
Murali Kakarla
Ranch Hand

Joined: Jul 11, 2007
Posts: 80
Thanks Mani. It worked

I really did
I think this is good experience to me before taking exam

Murali...
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140

Note that you can use the @Override annotation to catch such mistakes. The compiler would have raised an error.
 
 
subject: equals and hashcode methods
 
Similar Threads
What is wrong with my HashSet
chapter 7: K&B
Duplicates in HashSet
Problem in implementing a TreeSet