title: Mapping from String to integer - performance of various approaches
body: <p>Let's say that I need to make a mapping from <code>String</code> to an integer. The integers are unique and form a continuous range starting from 0. That is:</p>
  
  <pre><code>Hello -&gt; 0
  World -&gt; 1
  Foo   -&gt; 2
  Bar   -&gt; 3
  Spam  -&gt; 4
  Eggs  -&gt; 5
  etc.
  </code></pre>
  
  <p>There are at least two straightforward ways to do it. With a hashmap:</p>
  
  <pre><code>HashMap&lt;String, Integer&gt; map = ...
  int integer = map.get(string); // Plus maybe null check to avoid NPE in unboxing.
  </code></pre>
  
  <p>Or with a list:</p>
  
  <pre><code>List&lt;String&gt; list = ...
  int integer = list.indexOf(string); // Plus maybe check for -1.
  </code></pre>
  
  <p>Which approach should I use, and why? Arguably the relative performance depends on the size of the list/map, since <code>List#indexOf()</code> is a linear search using <code>String#equals()</code> -> O(n) efficiency, while <code>HashMap#get()</code> uses hash to narrow down the search -> certainly more efficient when the map is big, but maybe inferior when there are just few elements (there must be some overhead in calculating the hash, right?).</p>
  
  <p>Since benchmarking Java code properly is notoriously hard, I would like to get some educated guesses. Is my reasoning above correct (list is better for small, map is better for large)? What is the threshold size approximately? What difference do various <code>List</code> and <code>HashMap</code> implementations make?</p>