title: Generating hashCode() for a custom class
body: <p>I have a class named <code>Dish</code> and I handle it inside <code>ArrayList</code>s
  So I had to override default <code>hashCode()</code> method.</p>
  
  <pre><code>@Override
  public int hashCode() {
      int hash =7;
      hash = 3*hash + this.getId();
      hash = 3*hash + this.getQuantity();
      return hash;
  }
  </code></pre>
  
  <p>When I get two dishes with <code>id=4</code>,<code>quan=3</code> and <code>id=5</code>,<code>quan=0</code>, <code>hashCode()</code> for both is same;</p>
  
  <pre><code>((7*3)+4)*3+3 = 78
  ((7*3)+5)*3+0 = 78
  </code></pre>
  
  <p>What am I doing wrong? Or the magic numbers 7 and 3 I have chosen is wrong?</p>
  
  <p>How do I properly override <code>hashCode()</code> so that it generates unique hashes?</p>
  
  <p><strong>PS</strong>: From what I searched from google and SO, people use different numbers but the same method. If the problem is with the numbers, how do I wisely choose numbers that doesn't actually increase the cost for multiplication and at the same time works well even for more number of attributes.</p>
  
  <blockquote>
    <p>Say I had 7 int attributes and my second magic no. is 31, the final hash will be first magic no. * <strong>27512614111</strong> even if all my attributes are 0. So how do I do it <strong>without</strong> having my hashed value in <strong>billions</strong> so as keep my processor burden-free?</p>
  </blockquote>