• 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

Problem with "contains()" method of HashSet

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having a weird problem with the Contains method of an HashSet. My code involves comparing the "String" value of a variable "currentVar" in a set of "Strings" named "guardVars". Surprisingly, two string variables having the exact same value is returning different hashcode value, and I believe this is causing the problem. Please find my code and output-print statements below:




Output:


Current-Var: destinationFromPlaying
HC of Current-Var: 1945131690
Set: [destinationFromPlaying, varActivity]
S: destinationFromPlaying HC of S: -491443146
S: varActivity HC of S: -656537770



Please note that I did not override the equals method here and so, I am not required to override hashcode as well, I think.  Any explanation would be highly appreciated,  
Thank you!
 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you make a small, complete program that compiles, executes and shows the problem?
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set: [destinationFromPlaying, varActivity]

from what i learned, they are differernt strings, aren't they?
 
Bartender
Posts: 1845
10
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If they have different hash values, then they must be different string values.
They look the same....  but are they?
Try adding single quote delimiters to your println statements to see what you get:



Actually I am getting to like the string format capabilties of the printf (or String.format()):


 
Robin Atampore
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out the problem and in fact they are different strings. One of the strings had a following white-space which makes them different and it was hard to detect. Thank you very much for your reply!



Stefan Evans wrote:If they have different hash values, then they must be different string values.
They look the same....  but are they?
Try adding single quote delimiters to your println statements to see what you get:



Actually I am getting to like the string format capabilties of the printf (or String.format()):


 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic