• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

hashCode() method....pls help

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all,
can someone provide me an appropriate example to prove the below said.
This is mentioned in the java API,
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
also i want to know if there exists any special cases when the equals returns false and the hashCode() returns the same int value.
any comment would be a welcom.
Thanks in advance
Vijay Pillai
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code-

There isn't much to explain, so I'll allow you to experiment. If you still have questions, ask!

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay,
The rule is "If two objects are equal, that means if equals method return true, then they have the sam hash code"
-Faisal
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I think what Vijay wants is :
With two objects, equals method will return false
but their hashcode are equal.
As what the API said. The above possibility do exist.
 
Vijay pillai
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Wong,
you got it right, my question is still unanswered.
vijay Pillai
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on the hashing method being used. Anyone can invent a hashing algorithm. All the API says is that if you do invent one, it is alright if it converts more than one unique thing to the same hashcode (integer). After all, a hashcode is not attempting to encrypt the thing so that it can be properly decrypted later, it only exists to provide a sorting mechanism on the thing. Each time you run the thing through the algorithm it must produce the same hashcode, but if two things happen to calculate out to the same number nothing is broken.
Why don't you invent a simple hashcode algorithm and try it.
Say for Strings a=1,b=2,c=3 etc then add the numbers.
The word Vijay hashes out to 22+9+10+1+25 = 67;
The word yajiV hashes out to 25+1+10+9+22 = 67;
Using this hashcode algorithm both of these words would sort together in a hashset.
In real life the algorithms are significantly more complex of course, but the theory still holds true.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic