• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Uncertain about Set.contains()

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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??
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D. Ogranos wrote: . . . Good tip with the Override annotation, thanks again. . . .

 
I am going to test your electrical conductivity with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic