File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashCode Defination Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashCode Defination" Watch "HashCode Defination" New topic
Author

HashCode Defination

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

The equals() algorithm must be at least as precise in defining what
"meaningfully equivalent" means as the hashCode() method is.


what does this statement exactly mean ?


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I don't know, what is the intention of the author of that statement. But, generally, there is a contract between the hashCode() and equals() method for an object. We/developers should satisfy them in order to proper usages.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And don't forget to SayThanks You're leaving quite a lot of threads unanswered.


[My Blog]
All roads lead to JavaRanch
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438


well there is great example in the K & B

consider hashcode as a bucket where all the objects can be put
now
suppose we are putting Strings and the alphabets are given the numbers from 1 to 26
hashcode method defines the formula such that if the addition of the number allotted to all the letters is same
then the objects should be put in the same bucket
here
we take four names Alex, Dirk, Bob and Fred
Alex = 1 + 12 + 5 + 24 = 42
bob = 2 + 15 + 2 = 19 = 19
dirk = 4 + 9 + 18 + 11 = 42
Fred = 6 + 18 + 5 + 4 = 33

so this is our formula
and objects with equal hashcodes will go into same bucket

so here Alex and Dirk will go into the same bucket

now when we are implementing equals method
then it will check for the hashcode first
that means, the objects will be equal if the addition of numbers is equal
and they are in the same order
that means
for objects being equal, hashcodes SHOULD be equal

so, when we want to search equal objects, then objects with the same hashcode will be
searched instead of searching and calculating that value for all the objects

this increases the efficiency of program

hope this helps


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

THanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashCode Defination
 
Similar Threads
what is hashcode and equals contract?
hash code
hashcode,equals()
Enthuware mock test question (hashCode)
basicjava