Question: I know there are other questions about the general best practices while over riding hashCode and equals, but I have a very specific question. 

I have a class that has as an instance variable, an array of the same class. To be more explicit, here's the code:

```
Class Node{
    Node arr[] = new Node[5];
}

```

I need to overwrite hashCode for the class Node, and the array is an important, deciding factor in determining whether two Nodes are the same. How can I incorporate the array into the calculation of hashCode effectively?

--Edit--

I'm trying to check if the two nodes are the same, meaning that they have the same number of children, and that those children lead to the exact same states. Therefore, I'm effectively trying to compare the subtrees at the two node. I'm wondering if I can use hashing to do this equality check. 

I think I actually need to hash the entire subtree, but I'm not sure how I'd go about doing that given the recursive nature of my class definition.

Answer: 
It depends on what your criteria for equality are. Is the *order* in the array important? If so, you'll probably want to make the hash code depend on the order of the nodes in the array. If not, you may want to do something like XOR-ing the hash codes of all the nodes within the array. Presumably some of the values may be null (so be careful of that).

Basically, you need to override `hashCode` and `equals` consistently such that if two objects are equal, they will have the same hash code. That's the golden rule.

Eric Lippert has a [great blog post about `GetHashCode` in .NET](http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx) - the advice applies equally well for Java.

One potential problem to be aware of - if you end up with a cycle in your nodes (a reference to node A appearing in the array of node B and vice versa) you could end up having a cycle in the hash code calculation too.
A few of points to add to the current answers, if performance is of any concern.

First, you need to decide whether *order* of the child nodes in a node matter. If they don't, you cannot use the hashcode for an array. Consider fashioning your hashcode function around that defined by `java.util.Set`. Also consider using some ordering internally to improve equals performance. For example, if the subtrees' depths/heights differ, you can sort by depth.

Second, if your subtrees are deep, your hashcode can get very expensive. So I would cache the hashcode, and compute it upon construction (if your node is immutable), or invalidate upon mutation and re-calculate on demand.

Third, if your subtrees are deep, check the hashcode in equals() and return false early. Yes, hashcode is inspected by Map implementations, but there are places where code simply compares two objects using equals(), and they may pay a big price.

Finally, consider using Arrays.asList() (if child ordering matters) or HashSet (if ordering doesn't matter and no two child nodes are equal) instead of a simple array. Then equals and hashcode are reduced to delegating the call to the container instance... with appropriate caching of hashcode, of course.