• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

hashcode

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in case of Set and Maps does the hashcode and key have the same
maening . what does the hash code gives . ps explain me with an example.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Devesh,
No.
A hashcode relates to objects. A Set can have a hashcode and so can a Map. But the hashcode for the set or Map objects won't relate to the internals of the Set or Map. Each object contained in a Set or Map has its' own hashcode ...
The key is a string used by Maps to directly access a stored object. There might be some internal correlation between object hashcodes and keys but we as API users are not privy to that information.
As far as Java programming goes, Map keys and Object hashcodes are separate chapters of the same book ...
Regards,
Manfred.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
# hashcode
if the reference x and y denote two different objects
then the expression x.hashcode ()== y.hashcode() is always fasle
the above quote is from khalid mughal page 299 Q 10.2
I think that it is right b'coz hashcode returns unique for every object
but according to author it is not always right can anyone ps expalin
me why is this so
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No! the statement:
"if the reference x and y denote two different objects
then the expression x.hashcode ()== y.hashcode() is always false "
is incorrect. Although it is a programming objective to make hashcode generation create unique hashcodes, a hashcode is NOT guaranteed to be different.
Look in the Javadocs for the hashCode method in java.lang.Object for a nice discussion of the desired properties of a hashcode. This discussion specifically addresses the above statement.
Bill

------------------
author of:
 
rubbery bacon. rubbery tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic