• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashSet and equals()  RSS feed

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is probably a very basic question, but please help me understand where I'm going wrong. Please see below code -



I thought that if I override equals() method to compare if two objects of my User class are meaningfully equivalent, it'll give the same effect like Strings, when adding a similar object in a Set, but it's not. I tried a couple of things and in the end, simply returned "true" from the equals() method, but still the same result. What am I understanding/doing wrong here?

Thanks in advance!

ETA: Forgot to give the output - In the output, first set shows 2 elements "ABC" and "LMN", second set shows 2 elements 1 and 2, but third one shows 3 elements 2, 1, 1.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the "Beginning Java" forum since I don't see how this question relates to Swing or GUI programming and Beginning Java seems to be the best fit for such a question.
 
Rancher
Posts: 2887
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you didn't override the hash code as you expected because you spelt the method name wrong (you used a lower case "c"). Since the default hashcode implementation was used, the hashcode was unique for each object so there was no need to invoke the equals method. you should always use the @Override statement when overriding methods to avoid this problem in the future:

 
Roshni Singhania
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Thanks a lot for pointing that out! I checked equals() method signature 2-3 times before posting the question.. should've done that for hashCode() method too.. Quite a silly mistake! Dunno where to hide

Thanks again!
 
Marshal
Posts: 58454
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find out about the @Override annotation, which can save no end of embarrassment like that.
 
Roshni Singhania
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks for the link. Will go and study that now!
 
Campbell Ritchie
Marshal
Posts: 58454
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I would suggest always override hashcode() if you are overriding equals() and follow the contract which says if two objects are equal then there hashcode must be same. though its not required that if two objects are not equal then there hashcode must be different but its good to have to make it consistent.

Also think of compareTo() and compare() if you have data class like User in above case because you might need to provide sorting for your user either on natural order or in custom order.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!