Home » Archive

Articles Archive for September 2006

tools »

[2 Sep 2006 | Comments Off on hashCode() in Java 1.5 | ]

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, …