• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

HashCode and equals

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class B {
private int i1;
public int hashCode() {return 1;}
}
class C {
private int i1;
public int hashCode() {return -1;}
}
class D {
private int i1;
public int hashCode() {return i1;}
}

Suppose that the equals method of classes B, C and D all make use of the value of the int variable, i1. Which class has a hashCode method that is not consistent with the hash code contract?

a. B
b. C
c. D
d. None of the above

Please explain
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class B {
private int i1;
public int hashCode() {return 1;}
}
class C {
private int i1;
public int hashCode() {return -1;}
}
class D {
private int i1;
public int hashCode() {return i1;}
}

Suppose that the equals method of classes B, C and D all make use of the value of the int variable, i1. Which class has a hashCode method that is not consistent with the hash code contract?

a. B
b. C
c. D
d. None of the above

Please explain



I would say c.

Please consider the following code where the hashCode contract is violated becuase equals() method returns true but hashcodes are different..


[ April 24, 2007: Message edited by: megha joshi ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the original problem the class inherited Object.equals() implementation. By overriding equals() you have created a whole new problem. In the original problem D.hashCode() did not violate the hashCode contract.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right Megha,

As Rowe says if the class did not override the equals() method,
all three options would work. But your answer is consistent with
the question (i1 is in use by equals).


Good example as well!


Thanks,
cmbhatt
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose what you guys are saying is that class D did override of method equals(). You cant say that it didnt override equals and used the Object's equals() method. Becuase if you say so...Objects equals() is same as (==) opearator...Remember???Two objects would always be different even though they are meaninfully equal util you override eqauals().

The original question did not provide equals() implementation so I wrote it my way...The question was too broad.But if did provide overridden equals() as below.

Public boolean equals(Object o){
return this.i1 == o.i1;

}

Than the answer is D.

[ April 25, 2007: Message edited by: megha joshi ]
[ April 25, 2007: Message edited by: megha joshi ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Megha,

What Rowe said is standard scenario of question. But what question gives you
complete information. I was under the same impression when I replied first to
this question. And in fact I overlooked the line:


Suppose that the equals method of classes B, C and D all make use of the value of the int variable, i1.




Your answer is correct as per the question is concerned. Your example speaks
loudly about that!

Cheers!


Regards,
cmbhatt
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can you tell me whether wat I understud is correct.

1. If the classes B, C and D overrides the equals using i1, then class D is inconsistent with the Hashcode contract.

2. If non of the classes B,C and D provide the implementation of equals using i1, then all the three classes use the default Object's equals () and are consistent with the Hashcode contract.

But I feel,if all the classes override the equals using i1, class D follows the Hashcode contract.since it uses i1 in its Hashcode method.

Can anyone make me more clear about this.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sumi selva:
But I feel,if all the classes override the equals using i1, class D follows the Hashcode contract.since it uses i1 in its Hashcode method.

It depends on how you implement the equals method.
This example is correct
whereas this example violates the contract
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought also Answer is D.None of the above.
All are ok for contracts of equals and hashcode.
Because the i1 is private.So no other codes cann't change it.so contracts are ok for all 3 classes.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic