public class Node {
private Node nextNode;
@Override
public int hashCode() {
//How to implement this?
//Because you just have a attribute which is a reference.
//I think the attribute is almost useless, because if you use the HashCode of the attribute, you will finally fall into a useless loop.
//Thus, I think you should find a way to represent the HashCode of reference (instance) itself.
}
}
From the comment in the code, my question is actually how to uniquely identify reference itself, like address in C.
If you want the
Node‘s hash-code to represent its own reference — that is, if you’re not overridingequals(Object)— then you don’t need to overridehashCode()at all.If you want the
Node‘s hash-code to represent the reference of itsnextNode— that is, if yourequals(Object)looks something like this:— then you can use the JDK’s
System.identityHashCode(Object)utility method: