so, according the "contracts", if I not wrong, if hashCode == hashCode, equal must be equals
it's the other way round: if a.equals(b) then a.hashCode() == b.hashCode()
The hash code is some kind of very reduced "coarse id" of the object. Obviously, an int can not hold in all cases all information needed to distinguish the object from all other instances for all time.
E.g. a simple class with two int member variables can have 2^64 different instanciations, but a single integer can only be used to distinguish between 2^32 of them. [ December 20, 2005: Message edited by: Tilo Hemp ]
the hash code is used to determine the bucket( the place to store the keys)
suppose u have two objects whose hashcodes are equal but the objects are not equal as per the equals method
at this time first when u store the first object first the bucket is determined using the hashcode of this object,later the object is stored. when u store the second object similarly the hashcode of this object is determined since it has the same hashcode previously it checks wheather the object being stored are equal if true then it replaces the one which is stored first. if false then it stores the object in the same bucket, but while retriving the value first bucket is determined, since in this bucket there are two objects it uses the equals method to retrieve the value.this is legal, but this is inefficient in programming style. hope u got the solution if not query me about u r doughts
Want to add something- hash code is computed according to the following formula:
s*31^(n-1) + s*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
for answering such questions make sure that you cram up the table on K&B book page 418. in the real exam you wont get too much time to think on such questions,...it qould be better if you scribble the table while answering the exam
SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ] Performance is a compulsion, not a option, if my existence is to be justified.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com