Jdk源码详解之Iterable
接口
1. 接口释义
Implementing this interface allows an object to be the target of the "foreach" statement.
2. 源码
package java.lang;
import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
public interface Iterable<T> {
Iterator<T> iterator();
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
default Spliterator<T> spliterator() {
return Spliterators.spliteratorUnknownSize(iterator(), 0);
}
}
3. 方法简介
-
iterator()
方法
Iterator<T> iterator();
返回类型为T的元素的迭代器。【这里的T是泛型】