Iterable接口来自java.lang包,实现了Iterable接口的类可以使用for each去遍历。
Iterable接口通过iterator()方法返回一个Iterator实例
package java.lang;
/**
* Implementing this interface allows an object to be the target of
* the "for-each loop" statement.
*/
public interface Iterable<T> {
/**
* Returns an iterator over elements of type
*
* @return an Iterator.
*/
Iterator<T> iterator();
/**
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
}
原文:https://www.cnblogs.com/swifthao/p/12863355.html