title: Clean and performant way for iterating over an ArrayList
body: <p>First of all, note that there already are lots of similar questions on stackoverflow, e.g. <a href="https://stackoverflow.com/questions/99164/iterate-with-for-loop-or-while-loop">this</a>, <a href="https://stackoverflow.com/questions/1879255/performance-of-traditional-for-loop-vs-iterator-foreach-in-java">this</a>, <a href="https://stackoverflow.com/questions/89891/what-are-the-benefits-of-the-iterator-interface-in-java">this</a>. However, I'd like to highlight another aspect of the problem, I don't see answered in these discussions:</p>
  
  <p>Performing a benchmark of three variants of iterating over an <code>ArrayList</code> (specifically <code>ArrayList</code>, not other <code>List</code> implementations):</p>
  
  <p>Variant No. 1:</p>
  
  <pre><code>int size = list.size();
  for(int i = 0; i &lt; size; i++)
      doSomething(list.get(i));
  </code></pre>
  
  <p>Variant No. 2:</p>
  
  <pre><code>for(Object element : list)
      doSomething(element);
  </code></pre>
  
  <p>Variant No. 3:</p>
  
  <pre><code>list.forEach(this::doSomething);
  </code></pre>
  
  <p>It turns out that on my machine No. 1 and No. 3 perform equally, while No. 2 takes about 55% longer. This confirms the intuition, that the <code>Iterator</code> created implicitly in No. 2 does more work during each iteration, than the traditional loop over an array (for which there are decades of compiler optimization research). Variant No. 3 of course does the same loop as No. 1 internally.</p>
  
  <p>Now my actual question: Assuming that variant No. 3 is not applicable for some reason (side effects in the loop or no Java 8), what is the cleanest way of looping over a <code>List</code> in Java? As mentioned in <a href="https://stackoverflow.com/questions/1879255/performance-of-traditional-for-loop-vs-iterator-foreach-in-java">this</a> discussion, variant No. 1 is likely much less efficient for other <code>List</code>s than <code>ArrayList</code>. Keeping this in mind, we could do something like this:</p>
  
  <pre><code>if(list instanceof ArrayList)
      doVariant1(list);
  else
      doVariant2(list);
  </code></pre>
  
  <p>Unfortunately this rather smells like introducing boilerplate code. So, what would you recommend as a clean, as well as performant solution for iterating over a <code>List</code>?</p>
  
  <p><strong>EDIT</strong>: Here is the entire code for those wanting to reproduce the results. I am using JRE 1.8.0, JavaHotspot Sever VM build 25.0-b70 on MacOS 10 with 2 GHz Intel Core i7.</p>
  
  <pre><code>private static final int ARRAY_SIZE = 10_000_000;
  private static final int WARMUP_COUNT = 100;
  private static final int TEST_COUNT = 100 + (int)(Math.random() + 20);
  
  public void benchmark()
  {
      for(int i = 0; i &lt; WARMUP_COUNT; i++)
          test();
      long totalTime = 0;
      for(int i = 0; i &lt; TEST_COUNT; i++)
          totalTime += test();
      System.out.println(totalTime / TEST_COUNT);
  }
  
  private long test()
  {
      ArrayList&lt;Object&gt; list = new ArrayList&lt;&gt;(ARRAY_SIZE);
      for(int i = 0; i &lt; ARRAY_SIZE; i++)
          list.add(null);
      long t = System.nanoTime();
      doLoop(list);
      return System.nanoTime() - t;
  }
  
  private void doLoop(ArrayList&lt;Object&gt; list)
  {   // replace with the variant to test.
      for(Object element : list)
          doSomething(element);
  }
  
  private void doSomething(Object element)
  {
      Objects.nonNull(element);
  }
  </code></pre>