• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashset duplicates  RSS feed

 
Nitish Ratnaparkhi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I have a simple Dog class and a hashset.Despite of implementing Equals and hashset method in Dog I am getting duplicates in hashset.Is there anything I am missing here?



***********************************


*********************







Thanks.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitish

When posting code, always UseCodeTags <- link. I've added them for you this time, and doesn't it make it easier to read?

Check your spellings carefully. Or add the @Override annotation to all methods you believe you have overridden and let the compiler show you your mistake.
 
Nitish Ratnaparkhi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Silly Me....Thanks Darryl...yes ill use UseCodeTags from now on
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is recommended to add @Override annotation to the methods you have overridden.
The compiler would then help you to catch some mistakes.

e.g.

Your equals method is fine, but you have made a similar mistake.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't overriden hashcode() in your Dog class. Instead, you have a hascode() method.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have also overridden equals incorrectly. You must accept an Object; because you have the (Dog) cast as the first line in equals, you will suffer an Exception if you pass any Object which is not a Dog. You are not allowed to throw Exceptions, but should return false in those circumstances.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!