title: Why java 8 introduces iterable.forEach() loop even though it has for each?
body: <p>I'm not able to understand why java 8 has forEach loop and taking the Consumer functional interface as parameter. Even though we can do the same task using traditional for each loop without creating any extra overhead to create a class implements that from Consumer and implements a method and then pass this as reference to the forEach(). Although there is lambda expression to make it short.</p>
  
  <p>Q1- why iterable.forEach()?</p>
  
  <p>Q2. Where to use it?</p>
  
  <p>Q3. which one is faster traditional for each of Java 8 forEach()?</p>
  
  <p>Sample:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.function.Consumer;
  import java.lang.Integer;
  
  public class ForEachExample {
  
      public static void main(String[] args) {
  
          //creating sample Collection
          List&lt;Integer&gt; myList = new ArrayList&lt;Integer&gt;();
          for(int i=0; i&lt;10; i++) myList.add(i);
  
          //traversing using Iterator
          Iterator&lt;Integer&gt; it = myList.iterator();
          while(it.hasNext()){
              Integer i = it.next();
              System.out.println("Iterator Value::"+i);
          }
  
          //traversing through forEach method of Iterable with anonymous class
          myList.forEach(new Consumer&lt;Integer&gt;() {
  
              public void accept(Integer t) {
                  System.out.println("forEach anonymous class Value::"+t);
              }
  
          });
  
          //traversing with Consumer interface implementation
          MyConsumer action = new MyConsumer();
          myList.forEach(action);
  
      }
  
  }
  
  //Consumer implementation that can be reused
  **class MyConsumer implements Consumer&lt;Integer&gt;{
      public void accept(Integer t) {
          System.out.println("Consumer impl Value::"+t);
      }
  }**
  </code></pre>