Java hashcode based on identity
Posted
by hjfreyer
on Stack Overflow
See other posts from Stack Overflow
or by hjfreyer
Published on 2009-06-30T13:22:53Z
Indexed on
2010/04/21
10:13 UTC
Read the original article
Hit count: 266
The default behavior of Object.hashCode() is to return essentially the "address" of the object so that a.hashCode() == b.hashCode() if and only if a == b. How can I get this behavior in a user-defined class if a superclass already defines hashCode()? For instance:
class A {
public int hashCode() {
return 0;
}
}
class B extends A {
public int hashCode() {
// Now I want to return a unique hashcode for each object.
// In pythonic terms, it'd look something like:
return Object.hashCode(this);
}
}
Ideas?
© Stack Overflow or respective owner