aspose file tools*
The moose likes Beginning Java and the fly likes HashSet and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashSet and equals()" Watch "HashSet and equals()" New topic
Author

HashSet and equals()

Roshni Singhania
Ranch Hand

Joined: Mar 11, 2007
Posts: 78
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.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
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

Joined: Mar 11, 2007
Posts: 78
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!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39386
    
  28
Find out about the @Override annotation, which can save no end of embarrassment like that.
Roshni Singhania
Ranch Hand

Joined: Mar 11, 2007
Posts: 78
Hi Campbell,

Thanks for the link. Will go and study that now!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39386
    
  28
You're welcome
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


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.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashSet and equals()