title: Java8 forEach with index
body: <p>I was not able to find a forEach method which calls a lamda with the current object and the current index.</p>
  
  <p>Unfortunately this is not implemented in java8, so following implementation is not possible:</p>
  
  <pre><code>List&lt;String&gt; list = Arrays.asList("one", "two");
  list.forEach((element, index) -&gt; System.out.println(String.format("[%d] : %s", index, element)));
  </code></pre>
  
  <p>I know a simple way to do this is to use the for each loop with a index integer:</p>
  
  <pre><code>List&lt;String&gt; list = Arrays.asList("one", "two");
  
  int index = 0;
  for (String element : list) {
     System.out.println(String.format("[%d] : %s", index++, element));
  }
  </code></pre>
  
  <p>I think the common code to init a index intetger and to increment it for each iteration should be moved to a method. So I defined my own <code>forEach</code> method:</p>
  
  <pre><code>public static &lt;T&gt; void forEach(@NonNull Iterable&lt;T&gt; iterable, @NonNull ObjIntConsumer&lt;T&gt; consumer) {
      int i = 0;
      for (T t : iterable) {
          consumer.accept(t, i++);
      }
  }
  </code></pre>
  
  <p>And I can use it like:</p>
  
  <pre><code>List&lt;String&gt; list = Arrays.asList("one", "two");
  forEach(list, (element, index) -&gt; System.out.println(String.format("[%d] : %s", index, element)));
  </code></pre>
  
  <p>I was not able to find a similar implementation in any util library (e.g. guava).
  So I have following questions:</p>
  
  <ul>
  <li>Is there a reason why there is no an utility which provides me this functionality?</li>
  <li>Is there a  reason why this is not implemented in java <code>Iterable.forEach</code> methdod?</li>
  <li>Is there a good utility I didn't find which provides this functionality?</li>
  </ul>