title: Java : Iteration through a HashMap, which is more efficient?
body: <p>Given the following code, with two alternative ways to iterate through it,<br>
  <strong>is there any performance difference between these two methods?</strong></p>
  
  <pre><code>        Map&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;();
          //populate map
  
          //alt. #1
          for (String key : map.keySet())
          {
              Integer value = map.get(key);
              //use key and value
          }
  
          //alt. #2
          for (Map.Entry&lt;String, Integer&gt; entry : map.entrySet())
          {
              String key = entry.getKey();
              Integer value = entry.getValue();
              //use key and value
          }
  </code></pre>
  
  <p>I am inclined to think that <code>alt. #2</code> is the more efficient means of iterating through the entire <code>map</code> (but I could be wrong)</p>