Home » tools

hashCode() in Java 1.5

2 September 2006 No Comment

Can anybody think of a reason why you can’t just have a generic hashCode() utility in Java 1.5 that utilizes autoboxing and variable argument lists? I’ve been using the following code:

public static int computeHashCode(Object... o) {
int result = 17;
for (int i = 0; i < o.length; i++) {
result = 37 * result + (null == o[i] ? 0 : o[i].hashCode());
return result;

It could be called as follows:

public int hashCode() {
return computeHashCode(var1, var2, var3, var4, var5);

Because of autoboxing, it supports primitives and it seems like it would be easier and faster to use that the Jakarta Commons’ HashCodeBuilder.

Comments are closed.