Hashcode and equals
Solution 1:
Equality is only determined by method equals(). And method hashCode() is used in other situations, like by Map or Set. It is somewhat like a pre-condition or hint before actually calling equals (for efficiency). So it is assumed that if 2 objects are equal (that is, equals() returns true), then their hashCodes() must return the same value.
So in your code, 2 objects are equal, as long as your overriden equals() returns true, no matter what hashCode() does. hashCode() is not called at all when comparing for equality.
This question has more in-depth information regarding to the relationship between equals() and hashCode().
Solution 2:
First, in that line, you need to change Hashvalue
to HashValue
, since your class is actually called HashValue
.
Then, uncommenting the line gives you this:
public boolean equals(Object oo) {
if(oo instanceof HashValue)
HashValue hh = (HashValue)oo;
if (this.x==hh.x) {
return true;
} else {
return false;
}
}
There are a few things wrong with this:
This doesn't compile because
hh
isn't in scope when you end up using it.The first if statement should either make sure that the function doesn't run at all when you compare two things that aren't HashValues (i.e. throw an exception), or it should return
false
because HashValues are never equal to other types of objects. I generally prefer returningfalse
to throwing an exception.The second if statement is redundant, because you're just returning what the condition evaluates to.
Rework your method like this:
public boolean equals(Object oo) {
if(!(oo instanceof Hashvalue)) {
return false;
}
HashValue hh = (HashValue)oo;
return (this.x == hh.x);
}
This isn't quite right, either. To make sure that all equal objects have the same hash code, you have to override hashCode()
in HashValue
, and you must make sure that it lives up to the guarantee. Here, you could just add this:
// inside HashValue
int hashCode() {
return x;
}
The implementation is trivial because your object is just a wrapper around an int
. You'll need to think harder about this as your objects get more sophisticated.
Solution 3:
For starters, you need to capitalize the v in "Hashvalue"
if(oo instanceof Hashvalue)
should be
if (oo instanceof HashValue)
Solution 4:
HashValue and Hashvalue are two different identifiers
if(oo instanceof HashValue)
works because your class-name is HashValue
not Hashvalue
EDIT :
Your code doesn't work because hh
isn't in scope when you are using it.
This works:
/* A program to check hashcode values for object
@Author Myth17
*/
class HashValue
{
int x;
public boolean equals(Object oo)
{
HashValue hh=new HashValue();
if(oo instanceof HashValue)
hh = (HashValue)oo;
if(this.x==hh.x)
return true;
else
return false;
}
HashValue()
{
x=11;
}
}
class Hashing
{
public static void main(String args[])
{
HashValue hv=new HashValue();
HashValue hv2=new HashValue();
System.out.println(hv.hashCode());
System.out.println(hv2.hashCode());
if(hv.equals(hv2))
System.out.println("EQUAL");
else
System.out.println("NOT EQUAL");
}
}