1 2 class Foo { 3 private String string; 4 private Date date; 5 private i; 6 7 [...] 8 9 public int hashCode() { 10 int result; 11 result = (string != null ? string.hashCode() : 0); 12 result = 29 * result + (date != null ? date.hashCode() : 0); 13 result = 31 * result + (i != null ? i : 0); 14 [...] 15 return result; 16 }
Consider using larger prime numbers if the number of properties is large.
I tend to combine hash codes with ^. Arithmetic operations produce odd peaks if your hash codes aren't large enough to cause integer overflow when combined.