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

Uncertain about Set.contains()

D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214


Output is:
b.quals(a): true
Set size: 1
Set contains a: true
Set contains b: false

Why does the set not contain b? The objects a and b are equal (to the equals() method). The condition for contains(obj) method is "returns true if and only if there is an element e in the set such that obj.equals(e))" is true. That is the case, or am I thinking wrong??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
You have not correctly overridden the hashCode() method, so you can't expect a hash-based collection class to work correctly.
By the way: the instanceof operator is only reliable in the equals() method if the class is final.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Campbell Ritchie wrote:You have not correctly overridden the hashCode() method, so you can't expect a hash-based collection class to work correctly.
By the way: the instanceof operator is only reliable in the equals() method if the class is final.


I first had overriden hashcode() too, but that didn't change the output. I had implemented it as a method that just returned the key...that should work, shouldn't it? But I'll check again.

EDIT: DOH!! I had misspelled hashCode() as hashcode(). Of course that could not work...and I thought I had misunderstood something fundamentally. Thanks for quick answer anyway
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
You're welcome
If you had written a hashcode method, you ought to have shown us it. Because you didn't show it, I thought you hadn't written it.
You ought also to use the @Override annotation whenever you override a method; it is only available in Java5/6, but your hashcode method with the misspelling would have thrown a compiler error with @Override before it.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Campbell Ritchie wrote:You're welcome
If you had written a hashcode method, you ought to have shown us it. Because you didn't show it, I thought you hadn't written it.
You ought also to use the @Override annotation whenever you override a method; it is only available in Java5/6, but your hashcode method with the misspelling would have thrown a compiler error with @Override before it.


Good tip with the Override annotation, thanks again. I HAD written the hashcode method (with lowercase "c") first when I saw that the set didnt contain the element as I expected. But since the result didn't change when I wrote the method I figured the problem was something else and removed it again...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
D. Ogranos wrote: . . . Good tip with the Override annotation, thanks again. . . .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uncertain about Set.contains()