Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Inquisition qustion regarding equals() and hashCode()

Minhaj Mehmood
Ranch Hand
Posts: 400
Given two objects a and b of the same class, and if equals() and hashCode() are implemented correctly for that class, which of the following must hold true?

opt1: If a.hashCode() != b.hashCode() then !a.equals(b) #i agree its true!
opt2: If a.hashCode() == b.hashCode() then a.equals(b) # why its not true

Raju Champaklal
Ranch Hand
Posts: 521
if the hashcodes are same its not neccasary that both the objects are equals..they maybe be equals they may not be depending the way you have declared your equals method

Minhaj Mehmood
Ranch Hand
Posts: 400
Raju Champaklal wrote:if the hashcodes are same its not neccasary that both the objects are equals..they maybe be equals they may not be depending the way you have declared your equals method

was opt1 is not just opposite of the opt2?
I mean if
(a.hashCode() != b.hashCode()) == (!a.equals(b)) is true then
(a.hashCode() == b.hashCode()) == (a.equals(b)) why this is not true as well??

Marzio Let
Greenhorn
Posts: 1
No. Maybe if you write it on paper, with the usual logical symbol... it's an "if then" implication not an "if and only if": a equals b => a's hash code is equal to b's hash code. (TRUE) This can be written also: a's hash code not equal to b's hash code => a is not equal to b (TRUE).
But you can't deduce the opposite implication: a's H.C. == b's H.C. => a equals b (FALSE) This (false) statement can be written also as: a not equal b => a's H.C. != b's H.C. Maybe written this way it's easier to see because it's false (mind, I mean the implication is false; it may be true in some cases, but you can't be sure).

Minhaj Mehmood
Ranch Hand
Posts: 400

Note: The hashcode() and equals() generated through eclipse ide!

Ranch Hand
Posts: 156
Hi Minhaj,
First remember that if two objects hascode() method is equal, it doesn't necessarily mean that the two objects are equal.

Here a.hashCode()==b.hashCode() means you have a and b two objects are in same bucket not they are equal.

Minhaj Mehmood
Ranch Hand
Posts: 400
First remember that if two objects hascode() method is equal, it doesn't necessarily mean that the two objects are equal.

Here a.hashCode()==b.hashCode() means you have a and b two objects are in same bucket not they are equal.

Hi Rohan,

well if two objects hashCode() methos is equal - means the content of both object are EQUAL!

and what is the purpose of equals() method? that the content should be same! right?

if(a.equals(b) == true then its not mandatory
a == b
means the content could be same but object's memory reference could be different!!

Raju Champaklal
Ranch Hand
Posts: 521
suppose you add new Integer(4); to a set two times.....obviuosly a set returns false when you try to put in a duplicate....now when you put the 2nd Integer(4) inside the set first the Set calls the hashcode on every object already present in the set....if the hashcode of the object you want to put and hashcode of any object already there in the set matches then equals method of those 2 objects having similar hashcodes are called....if the equals() returns true on both those objects than the both the objects are considered equals and the set does not add the duplicate to the set by returning false

now since in Integer class the hashcode and equals method are overloaded the Set understands what a duplicate and you dont have to override them in your class

but if you have made a class by yourself and if you dont put hashcodes method but only equals method than no object will be considered equal because first hashcode() is called which returns a unique number everytime...so if you want to put objects in a collection ad you want to prevent duplicates getting inside override both the hashcode and the equals method...overriding just one wont prevent the duplication occur.....but if your not using collections you can make your own equals method according to your programs need

understood???

Ranch Hand
Posts: 156
Minhaj kaimkhani wrote:
First remember that if two objects hascode() method is equal, it doesn't necessarily mean that the two objects are equal.

Here a.hashCode()==b.hashCode() means you have a and b two objects are in same bucket not they are equal.

Hi Rohan,

well if two objects hashCode() methos is equal - means the content of both object are EQUAL!

and what is the purpose of equals() method? that the content should be same! right?

if(a.equals(b) == true then its not mandatory
a == b
means the content could be same but object's memory reference could be different!!

Note that two objects hashcode() methods is equals - means they point to same bucket by some hashing criteria defined in hashCode() mehod

Raju Champaklal
Ranch Hand
Posts: 521
if you are making your own class and you yourself want to check if two objects are equals you dont want to implement hashcodes...just equals will do it...becasue when you call equals method in your class no hashcodes is called...but in a collection hashcodes is always called

Minhaj Mehmood
Ranch Hand
Posts: 400
can anyone show me such code where:

a.hashCode() == b.hashCode() comes true
then
a.equals(b) does not come true.

because in my code(see above)
a.equals(b) is always true when a's and b's hash codes are same!

Minhaj

Raju Champaklal
Ranch Hand
Posts: 521

this is wrong my friend....

and a.equals(b) if true doesnt mean the content of both the objects is same....if you are talking about string class or integer class or java classes then the content is same but if you are talking about classes made by the user then equals method is dependent on hoe you have defined the equals method in your class

Raju Champaklal
Ranch Hand
Posts: 521

Minhaj Mehmood
Ranch Hand
Posts: 400
Given two objects a and b of the same class, and if equals() and hashCode() are implemented correctly for that class, which of the following must hold true?

check the question! equals() and hashCode() must be implemented true!

Ranch Hand
Posts: 156

Minhaj Mehmood
Ranch Hand
Posts: 400

but is this the correct implementation of equals() and hashCode(), I mean its totally legal, but examiner said where "hashCode() & equals() implemented correct"l!!

Raju Champaklal
Ranch Hand
Posts: 521
what do you think the examiner mean exactly...maybe we can discuss the possibilities

Minhaj Mehmood
Ranch Hand
Posts: 400
Raju Champaklal wrote:what do you think the examiner mean exactly...maybe we can discuss the possibilities

I thought correctly means the way i implemented!!

Raju Champaklal
Ranch Hand
Posts: 521
how can this be the right implementation of equals method when you are not using it in the right way and returingn true everytime....you arent even comparing the variables of different objects...you are not comparing anything....how can you call it right implementation?

Minhaj Mehmood
Ranch Hand
Posts: 400
Raju Champaklal wrote:how can this be the right implementation of equals method when you are not using it in the right way and returingn true everytime....you arent even comparing the variables of different objects...you are not comparing anything....how can you call it right implementation?

Isn't it right? Can you explain a bit more?

Raju Champaklal
Ranch Hand
Posts: 521
dude i am sorry i lose....you won...congrats....i cant take it anymore..any questions of hashocde and equals cming in exam would be left by me

Minhaj Mehmood
Ranch Hand
Posts: 400
Raju Champaklal wrote:dude i am sorry i lose....you won...congrats....i cant take it anymore..any questions of hashocde and equals cming in exam would be left by me

Oh come on dude!! the discussion is not about win or lose!! i was just trying to clear my doubts nothing else!!

Raju Champaklal
Ranch Hand
Posts: 521
this is from whizlabs....hope this will clear you...how shoudl i upload the image?

Neha Daga
Ranch Hand
Posts: 504
lets see Rohan's code:

his hashcode method says: two animals having same name length will return equal hashcode i.e if animal1 has name 'juno' and animal2 has name 'luno' then the hashcode will be equal.

his equal method syas: two animal objects will be equal only if they have same name.now animal1 and animal2 dont have same name so they can't be equal.

in this case it is clear that animal1 and animal2 are not equal inspite of having equal hashcodes.

remember as rohan says:

Two objects having same hashcodes are in same bucket that doesn't mean they are equal.

their being equal depends only on how class' equal method is implemented.

Minhaj Mehmood
Ranch Hand
Posts: 400
another question raised into my mind:
the methods equals() and hashCode() in my 5th code are implemented correctly enough efficient as stated into K&B chap 7??

Minhaj