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

Print this page
rev 6197 : [mq]: collections


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.util.Iterator;


  29 
  30 /**
  31  * Implementing this interface allows an object to be the target of
  32  * the "foreach" statement.
  33  *
  34  * @param <T> the type of elements returned by the iterator
  35  *
  36  * @since 1.5
  37  */
  38 public interface Iterable<T> {
  39 
  40     /**
  41      * Returns an iterator over a set of elements of type T.
  42      *
  43      * @return an Iterator.
  44      */
  45     Iterator<T> iterator();














  46 }


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.util.Iterator;
  29 import java.util.Objects;
  30 import java.util.function.Block;
  31 
  32 /**
  33  * Implementing this interface allows an object to be the target of
  34  * the "foreach" statement.
  35  *
  36  * @param <T> the type of elements returned by the iterator
  37  *
  38  * @since 1.5
  39  */
  40 public interface Iterable<T> {
  41 
  42     /**
  43      * Returns an iterator over a set of elements of type T.
  44      *
  45      * @return an Iterator.
  46      */
  47     Iterator<T> iterator();
  48 
  49     /**
  50      * Execute the specified Block for each element
  51      *
  52      * @param block The Block to which elements will be provided
  53      * @throws NullPointerException if the specified block is null
  54      * @since 1.8
  55      */
  56     public default void forEach(Block<? super T> block) {
  57         Objects.requireNonNull(block);
  58         for (T t : this) {
  59             block.accept(t);
  60         }
  61     }
  62 }