title: Using HashMap to count instances
body: <p>I have the following code to count the instances of different strings in an array;</p>
  
  <pre><code>String words[] = {"the","cat","in","the","hat"};
  HashMap&lt;String,Integer&gt; wordCounts = new HashMap&lt;String,Integer&gt;(50,10);
  for(String w : words) {
      Integer i = wordCounts.get(w);
      if(i == null) wordCounts.put(w, 1);
      else wordCounts.put(w, i + 1);
  }
  </code></pre>
  
  <p>Is this a correct way of doing it? It seems a bit long-winded for a simple task. The <code>HashMap</code> result is useful to me because I will be indexing it by the string.</p>
  
  <p>I am worried that the line</p>
  
  <pre><code>else wordCounts.put(w, i + 1);
  </code></pre>
  
  <p>could be inserting a second <code>key-value</code> pair due to the fact that</p>
  
  <pre><code>new Integer(i).equals(new Integer(i + 1));
  </code></pre>
  
  <p>would be false, so two <code>Integers</code> would end up under the same <code>String</code> key bucket, right? Or have I just over-thought myself into a corner?</p>