• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

equal() and hashcode()..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I got it from Master Exam.
11.x=0;
12.if(x1.hashcode()!=x2.hashcode())x=x+1;
13.if(x3.equals(x4)==false)x=x+10;
14.if(x5.equals(x6)==true)x=x+100;
15.if(x7.hashcode()==x8.hashcod())x=x+1000;
16.System.out.println("x=" + x);

if the output is "x=1111", which of the following will always be true?

Answer given: x2.equals(x1)==true
I think this answer violates the contract.the correct answer would be x5.hashcode()==x6.hascode().
other than "x2.equals(x1)" the rest are coming under "can be true" so not suitable for the question asked.

please do correct me if i am wrong.

Thanks
Preetha
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I guess it would be better if you can give all the options.
Then it would be easier!
 
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Under the given circumstances, Arun is right. Since the hashCodes don't match, so the objects can never be equal...
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pritha,
you are right.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi preetha,

yes the answer is x5.hashcode==x6.hashcode and its given correct in master exam.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like M Srilatha, I would also like to see all of the original choices for the answers to the question.

There is a good Mock exam (7 questions) on the hashCodes and equals contract for Inquisition (http://enigma.vm.bytemark.co.uk/inquisition/index.php/Main_Page).

I find that there are really only 2 rules to remmber, i.e.
Rule 1 if x1.equals(x2)==true then x1.hashCode()==x2.hashCode()
Rule 2 if x1.hashCode()!=x2.hashCode() then x1.equals(x2)==false

because if x1.equals(x2)==false
then nothing can be deduced about hashCode()
and if x1.hashcode()==x2.hascode()
then nothing can be deduced about equals()

Please someone correct me if I have incorrectly oversimplified it.

Using these rules on the problem as given,

line 12,
12. if(x1.hashcode()!=x2.hashcode())x=x+1;
easily disproves the answer:
x2.equals(x1)==true

and line 14,
14. if(x5.equals(x6)==true)x=x+100;
easily proves the answer:
x5.hashcode()==x6.hascode()
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules to remember are:

If x.equals(y)==true then x.hashCode()==y.hashCode() MUST be true;
If x.equals(y)==false then x.hashCode()==u.hashCode() MIGHT be true;

And these rules are applied on Collections that uses Hash Codes for fast searching, because they first search the bucket (hashCode) before search the object (equals), otherwise equals() have nothing to do with hashCode().

For instance:



They will be equals even with differente hashCodes, but they will not be found in a collection that uses hash codes for fast searching using its own implemented methods.
[ December 05, 2008: Message edited by: Fabio Nascimento ]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hay ranchers,

this is not a big problem if you know little pure mathematics

If A implies B

we can say (Not)B implies (Not)A

thats all to remember

we can think A as equals() method return true and
B as two hashcode values are same


hopes you can understand
 
reply
    Bookmark Topic Watch Topic
  • New Topic