/*
* 版权所有 (c) 2003, 2013, 甲骨文和/或其子公司。保留所有权利。
* 甲骨文专有/保密。使用是受许可条款的规限。
*/
package java.lang;
import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
/**
* 实现此接口允许对象成为 "for-each loop" 语句的目标。 * the "for-each loop" statement. See
* <strong>
* <a href="/go.html?url={@docRoot}/../technotes/guides/language/foreach.html">For-each Loop</a>
* </strong>
*
* @param <T> the type of elements returned by the iterator
*
* @since 1.5
* @jls 14.14.2 The enhanced for statement
*/
public interface Iterable<T> {
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
Iterator<T> iterator();
/**
* Performs the given action for each element of the {@code Iterable}
* until all elements have been processed or the action throws an
* exception. Unless otherwise specified by the implementing class,
* actions are performed in the order of iteration (if an iteration order
* is specified). Exceptions thrown by the action are relayed to the
* caller.
*对 Iterable 的每个元素执行给定的操作, 直到处理完所有元素或操作引发异常。除非实现类另有指定,
*否则将按迭代顺序执行操作 (如果指定了迭代顺序)。操作引发的异常将中继给调用方。
* @implSpec
* <p>默认实现的行为就像:
* <pre>{@code
* for (T t : this)
* action.accept(t);
* }</pre>
*
* @param action 要为每个元素执行的操作
* @throws NullPointerException 如果指定的操作为 null
* @since 1.8
*/
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
/**
* 在此 Iterable 描述的元素上创建一个 Spliterator/*
* 版权所有 (c) 2003, 2013, 甲骨文和/或其子公司。保留所有权