Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

equals and hashcode methods  RSS feed

 
Murali Kakarla
Ranch Hand
Posts: 80
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing hashcode to hashCode!
 
Murali Kakarla
Ranch Hand
Posts: 80
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!