Calling T[].hashCode()
(where T
is some class) will not take into account the contents of the array.
Use the Arrays.hashCode(Object[])
static method to calculate an array's hash code instead.
Any array type inherits the default equals()
and hashCode()
implementations from Object
. By default, object equality works by comparing reference addresses (like a == b
), and an object's hash code can also similarly depend on its address (The JVM specification leaves this at the discretion of the implementation). Make sure this is the behavior you actually want.
String[] names = ...;
int namesHash = names.hashCode(); // Bad.
int namesHash = Arrays.hashCode(names);
If you actually intend to use the default hashCode
implementation of arrays, you may safely ignore this issue.
java.lang.Object.hashCode()
java.util.Arrays.hashCode()