title: Which is more efficient, a for-each loop, or an iterator?
body: <p>Which is the most efficient way to traverse a collection?</p>
  
  <pre><code>List&lt;Integer&gt;  a = new ArrayList&lt;Integer&gt;();
  for (Integer integer : a) {
    integer.toString();
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>List&lt;Integer&gt;  a = new ArrayList&lt;Integer&gt;();
  for (Iterator iterator = a.iterator(); iterator.hasNext();) {
     Integer integer = (Integer) iterator.next();
     integer.toString();
  }
  </code></pre>
  
  <p>Please note, that this is not an exact duplicate of <a href="https://stackoverflow.com/questions/89891/what-are-the-benefits-of-the-iterator-interface-in-java">this</a>, <a href="https://stackoverflow.com/questions/131241/why-use-iterators-instead-of-array-indices">this</a>, <a href="https://stackoverflow.com/questions/99164/iterate-with-for-loop-or-while-loop">this</a>, or <a href="https://stackoverflow.com/questions/1879255/traditional-for-loop-vs-iterator-in-java">this</a>, although one of the answers to the last question comes close. The reason that this is not a dupe, is that most of these are comparing loops where you call <code>get(i)</code> inside the loop, rather than using the iterator.</p>
  
  <p>As suggested on <a href="https://meta.stackexchange.com/questions/12513/stackoverflow-should-i-answer-my-own-question-or-not">Meta</a> I will be posting my answer to this question.</p>