File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals and hashcode methods

 
Murali Kakarla
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing hashcode to hashCode!
 
Murali Kakarla
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mani. It worked

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

Murali...
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Note that you can use the @Override annotation to catch such mistakes. The compiler would have raised an error.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic