title: IntelliJ IDEA suggests replacing for loops with foreach method. Should I always do that when possible?
body: <p>IDEA suggests to replace, for example, this:</p>
  
  <pre><code>for (Point2D vertex : graph.vertexSet()) {
    union.addVertex(vertex);
  }
  </code></pre>
  
  <p>with this:</p>
  
  <pre><code>graph.vertexSet().forEach(union::addVertex);
  </code></pre>
  
  <p>This new version is sure much more readable. But are there situations when I'd better stick to the good old language construct for iteratables rather than using the new <code>foreach</code> method?</p>
  
  <p>For instance, if I understand correctly, the method reference mechanism implies constructing an anonymous <code>Consumer</code> object that otherwise (with <code>for</code> language construct) would not be constructed. Could that become a performance bottleneck for some actions?</p>
  
  <p>So I wrote this not very exhaustive benchmark:</p>
  
  <pre><code>package org.sample;
  
  import org.openjdk.jmh.annotations.Benchmark;
  import org.openjdk.jmh.annotations.Fork;
  import org.openjdk.jmh.annotations.Threads;
  import org.openjdk.jmh.infra.Blackhole;
  import org.tendiwa.geometry.Point2D;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.stream.Collectors;
  import java.util.stream.IntStream;
  
  public class LanguageConstructVsForeach {
      private static final int NUMBER_OF_POINTS = 10000;
      private static final List&lt;Point2D&gt; points = IntStream
          .range(0, NUMBER_OF_POINTS)
          .mapToObj(i -&gt; new Point2D(i, i * 2))
          .collect(Collectors.toList());
  
      @Benchmark
      @Threads(1)
      @Fork(3)
      public void languageConstructToBlackhole(Blackhole bh) {
          for (Point2D point : points) {
              bh.consume(point);
          }
      }
      @Benchmark
      @Threads(1)
      @Fork(3)
      public void foreachToBlackhole(Blackhole bh) {
          points.forEach(bh::consume);
      }
      @Benchmark
      @Threads(1)
      @Fork(3)
      public List&lt;Point2D&gt; languageConstructToList(Blackhole bh) {
          List&lt;Point2D&gt; list = new ArrayList&lt;&gt;(NUMBER_OF_POINTS);
          for (Point2D point : points) {
              list.add(point);
          }
          return list;
      }
      @Benchmark
      @Threads(1)
      @Fork(3)
      public List&lt;Point2D&gt; foreachToList(Blackhole bh) {
          List&lt;Point2D&gt; list = new ArrayList&lt;&gt;(NUMBER_OF_POINTS);
          points.forEach(list::add);
          return list;
      }
  
  }
  </code></pre>
  
  <p>And got: </p>
  
  <pre><code>Benchmark                                                       Mode  Samples      Score     Error  Units
  o.s.LanguageConstructVsForeach.foreachToBlackhole              thrpt       60  33693.834 ± 894.138  ops/s
  o.s.LanguageConstructVsForeach.foreachToList                   thrpt       60   7753.941 ± 239.081  ops/s
  o.s.LanguageConstructVsForeach.languageConstructToBlackhole    thrpt       60  16043.548 ± 644.432  ops/s
  o.s.LanguageConstructVsForeach.languageConstructToList         thrpt       60   6499.527 ± 202.589  ops/s
  </code></pre>
  
  <p>How comes <code>foreach</code> is more efficient in both cases: when I do virtually nothing and when I do some actual work? Doesn't <code>foreach</code> simply encapsulate <code>Iterator</code>? Is this benchmark even correct? If it is, is there any reason today to use the old language construct with Java 8?</p>