Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

need help on hashCode()

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys ,

i really find it difficult to find which ( of the options given ) is correct implementation of hashCode() method, provided you are given
an equals() implementation

The following question is taken for Khalid Mughal mock exam


1) return 0;
2) return a;
3) return a+b;
4) return a-b;
5) return a^b;
6) return (a 16)|b;

First of all..."WHAT THE HELL IS THAT LAST OPTION ??? "

Ok, as per the hashCode( ) contract - "If two objects are equal as per the equals ( ) method (which we override in our class), then a hashCode( ) method called on each of these objects must return identical values"

Since the converse statement ("Unequal objects may return equal hashCodes" is not a part of contract), in order to find out which of the above options is correct, the BEST and the ONLY approach to solve the above question is to scrutinize the cases when the return statment in equals( ) method returns "true" ,and as per these case imagine proper values of a,b and finally try each option to see whether the option satisfies the contract.

I think my approach is correct. But still i am facing probelem in landing at correct answers

"Identifying which is correct implementation of hashCode( ) contract" is the most challenging question in scjp exam

Please help for above question

Thanks in advance
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok, as per the hashCode( ) contract - "If two objects are equal as per the equals ( ) method (which we override in our class), then a hashCode( ) method called on each of these objects must return identical values"



Now,

You have to be carefully need to check the conditions on which equals method will return true,

Now , consider

Two Object of the class OBJ1 & OBJ2

Condition 1
-----------
say OBJ1 --> a=1,b=2, ......... & OBJ2-->a=1,b=2
Now evalute (a == o.a && b == o.b) || (a == o.b && b == o.a);
Here First operation of || will return true.

so, OBJ1.equals(OBJ2) will return true.
so, we can say if equals() return true then the hashcode() method will return fixed or same value,
under this circumstances,
2) return a;
3) return a+b;
4) return a-b;
5) return a^b;

all are right.

But this is not the final answer. wait.

Condition 2
----------
say OBJ1 --> a=1,b=2, ......... & OBJ2-->a=2,b=1
Now evalute (a == o.a && b == o.b) || (a == o.b && b == o.a);
Here Second operation of || will return true.

so, OBJ1.equals(OBJ2) will return true.
so, we can say if equals() return true then the hashcode() method will return fixed or same value,
under this circumstances,

3) return a+b;
5) return a^b;

Condition 3
----------
say OBJ1 -->a=1,b=2, ......... & OBJ2-->a=10,b=20
so,
Now evalute (a == o.a && b == o.b) || (a == o.b && b == o.a);
so OBJ1.equals(OBJ2) will never return true.
so,
1) return 0; can never be an option.


So,
Ultimately the final answer is
3) return a+b;
5) return a^b;
[ November 25, 2005: Message edited by: Purujit Saha ]
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also visit this link,
http://www.javaranch.com/journal/200407/Journal200407.jsp#a1
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Purujit Saha:

Nice article. But I could not understand one thing. When the author about "Faulty hashCodes", in the example given :



Does the objects have the same keys. I don't think so. The hashCode() are different for the objects but the author states that the hashCodes are same.

Or have I read it incorrectly. I read it thrice.

regards,
vijay.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi purujit,

i agree with all the three cases. But the conclusion,
which you drew from the last case,is incorrect

This is because, return 0; will always return same hash
code (i.e 0) for equal and unequal objects. Since
"equal objects must have equal hash codes" is the only
thig that we need to worry as far the contract is
concerned, return 0; is the third correct option

The other two correct options are return a+b;
and return a^b; as you worked out.

The cases that you choose to arrive at the answers are
nice n perfect..but think the handle this scene -& in place of && and | in place of ||

So as far as && and || are there in the hash code implementation, what i think that
the conditons to be worked out are more. This because of the following

1) && looks for 'false' as the first operand, and returns false if the first opearand
is 'false' i.e it doesnt waste time in checking whether the 2nd opearand is true or false
2nd operand is checked only if the first one is 'true'
2) || looks for 'true' as the first operand, and returns true if the first opearand
is 'true' i.e it doesnt waste time in checking whether the 2nd opearand is true or false.
3nd operand is evaluated only if the first operand is false.
3) WE need to TARGET ALL POSSIBLE CASES (under the light of points 1 and 2)
that make the equals( ) method return true. We then need to sort the given answers
which satisfy the contract.

So,purujit,under the light of above three points, i need we need to restructure the
conditions provided by you in the previous reply.

Please help me

if possible send me ur reply on niranjan_8712@yahoo.com
this will help us communincate better on a 1 to 1 basis


thanks in advance
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi purujit,

i agree with all the three cases. But the conclusion,
which you drew from the last case,is incorrect

This is because, return 0; will always return same hash
code (i.e 0) for equal and unequal objects. Since
"equal objects must have equal hash codes" is the only
thig that we need to worry as far the contract is
concerned, return 0; is the third correct option

The other two correct options are return a+b;
and return a^b; as you worked out.

The cases that you choose to arrive at the answers are
nice n perfect..but think the handle this scene -& in place of && and | in place of ||

So as far as && and || are there in the hash code implementation, what i think that
the conditons to be worked out are more. This because of the following

1) && looks for 'false' as the first operand, and returns false if the first opearand
is 'false' i.e it doesnt waste time in checking whether the 2nd opearand is true or false
2nd operand is checked only if the first one is 'true'
2) || looks for 'true' as the first operand, and returns true if the first opearand
is 'true' i.e it doesnt waste time in checking whether the 2nd opearand is true or false.
3nd operand is evaluated only if the first operand is false.
3) WE need to TARGET ALL POSSIBLE CASES (under the light of points 1 and 2)
that make the equals( ) method return true. We then need to sort the given answers
which satisfy the contract.

So,purujit,under the light of above three points, i need we need to restructure the
conditions provided by you in the previous reply.

Please help me

if possible send me ur reply on niranjan_8712@yahoo.com
this will help us communincate better on a 1 to 1 basis


thanks in advance
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Niranjan Deshpande

You are correct.


The general contract of hashCode is:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.




As per the third contract my conclution for the Condition 3 is incorrect. I apologies for that.It is applicable if the question is related to performance point of view.

Thnx for your correction.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know that contract stuff,,,

you didnt say anything on the cases that need to be modified since the operators are && and ||
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since dicussion stuff here in not about operator && or ||


class ValuePair { public int a, b; public boolean equals(Object other) { try { ValuePair o = (ValuePair) other; return (a == o.a && b == o.b) || (a == o.b && b == o.a); } catch (ClassCastException cce) { return false; } } public int hashCode() { // Provide implementation here. }}



Here we have to test all the cases when the equals methods return true.Based on that we have to reach out the correct hashcode() implementation.

// return (a == o.a && b == o.b) || (a == o.b && b == o.a);

Since both side of "&&" operator need to be true for a output of true & any of the side of "||" operator needs to be true for an output of true,i took those conditions only for which the above condition will return true.

I think in my previous explanation,
Condition 1 & Condition 2 are those cases, Other then that there will not be any other cases for which the above condition will return true.

About my Condition 3 , i already expressed my appology.
[ November 28, 2005: Message edited by: Purujit Saha ]
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic