src/share/classes/java/lang/Iterable.java

Print this page
rev 6197 : [mq]: collections

@@ -24,10 +24,12 @@
  */
 
 package java.lang;
 
 import java.util.Iterator;
+import java.util.Objects;
+import java.util.function.Block;
 
 /**
  * Implementing this interface allows an object to be the target of
  * the "foreach" statement.
  *

@@ -41,6 +43,20 @@
      * Returns an iterator over a set of elements of type T.
      *
      * @return an Iterator.
      */
     Iterator<T> iterator();
+
+    /**
+     * Execute the specified Block for each element
+     *
+     * @param block The Block to which elements will be provided
+     * @throws NullPointerException if the specified block is null
+     * @since 1.8
+     */
+    public default void forEach(Block<? super T> block) {
+        Objects.requireNonNull(block);
+        for (T t : this) {
+            block.accept(t);
+        }
+    }
 }