public interface Iterable{ /** * Returns an iterator over elements of type {@code T}. * * @return an Iterator. */ Iteratoriterator(); /** * 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. * * @implSpec *
The default implementation behaves as if: *{@code * for (T t : this) * action.accept(t); * }* * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEach(Consumeraction) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } /** * Creates a {@link Spliterator} over the elements described by this * {@code Iterable}. * * @implSpec * The default implementation creates an *early-binding* spliterator from the iterable's {@code Iterator}. The spliterator * inherits the fail-fast properties of the iterable's iterator. * * @implNote * The default implementation should usually be overridden. The * spliterator returned by the default implementation has poor splitting * capabilities, is unsized, and does not report any spliterator * characteristics. Implementing classes can nearly always provide a * better implementation. * * @return a {@code Spliterator} over the elements described by this * {@code Iterable}. * @since 1.8 */ default Spliteratorspliterator() { return Spliterators.spliteratorUnknownSize(iterator(), 0); } }
这意味着List和Set接口也会继承该方法,比较常用的两个间接实现类ArrayList和HashSet中的iterator方法实现就是给构造并返回一个迭代器对象。
public class ArrayListextends AbstractListimplements List, RandomAccess, Cloneable, java.io.Serializable{ ... ... /** * Returns an iterator over the elements in this list in proper sequence. * *
The returned iterator isfail-fast. * * @return an iterator over the elements in this list in proper sequence */ public Iteratoriterator() { return new Itr(); } /** * An optimized version of AbstractList.Itr */ private class Itr implements Iterator{ // Android-changed: Add "limit" field to detect end of iteration. // The "limit" of this iterator. This is the size of the list at the time the // iterator was created. Adding & removing elements will invalidate the iteration // anyway (and cause next() to throw) so saving this value will guarantee that the // value of hasNext() remains stable and won't flap between true and false when elements // are added and removed from the list. protected int limit = ArrayList.this.size; int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; public boolean hasNext() { return cursor= limit) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } public void remove() { if (lastRet