title: Java String HashCode of n digits?
body: <p>I'm trying to get an ID from a String in Java, and I thought I would use hashcode (Yeah, two strings can have the same hashcode but I can live with that small probability). I want this ID to have a max of 4 digits. Is that possible?</p>
  
  <p>This is the String default hashCode implementation:</p>
  
  <pre><code>public int hashCode() {
      int h = hash;
      if (h == 0 &amp;&amp; value.length &gt; 0) {
          char val[] = value;
  
          for (int i = 0; i &lt; value.length; i++) {
              h = 31 * h + val[i];
          }
          hash = h;
      }
      return h;
  }
  </code></pre>
  
  <p>Can I override it to produce a hash with 4 digits?</p>