This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode" Watch "hashcode" New topic
Author

hashcode

devesh singh
Greenhorn

Joined: May 08, 2001
Posts: 9
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.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
nishesh chouhan
Ranch Hand

Joined: Nov 06, 2000
Posts: 37
# 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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
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:


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode
 
Similar Threads
hashCode()????
Help - Exam Watch about Collection -K&B
need help with the space-time continium
Overriding hashCode()
hashCode()