File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HashSet not using overridden equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashSet not using overridden equals method" Watch "HashSet not using overridden equals method" New topic
Author

HashSet not using overridden equals method

Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

with regards to this problem, I have discovered that HashSet.contains( Object o ) is not calling my .equals( Object o ) method that I've written for it. Is there any scenario in which HashSet.contains( Object obj ) would not invoke the obj.equals( hashset_element ) ?
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

actually, now that I think of it, could my hashCode() method implementations be incorrect so they aren't checking in the right bucket for the element?
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Originally posted by Jamie Robertson:
actually, now that I think of it, could my hashCode() method implementations be incorrect so they aren't checking in the right bucket for the element?

FOUND IT!!! damn hashCode() implementation all along. It was correct for checking equality within the same class, but when it tried to check equality with my second allowable class ( with its own different hashCode() implementation ) it couldn't find the right bucket to then check equality. One night of sleep, plus 6 minutes and problem solved!! it's too bad I don't get more sleep
Jamie
 
jQuery in Action, 2nd edition
 
subject: HashSet not using overridden equals method
 
Similar Threads
Difference in equals() method and comparision operator
.equals() question
contract for equals and hashcode
Sorting objects based on potentially null property values
Uniqueness of static variable