版本 JDK8(JDK1.8)
Iterator接口源码重点 1.Iterator接口的扩展版是ListIterator接口,ListIterator允许沿着两个方向遍历列表(向后next()和向前previous()),同时比Iterator,多了set(.),add(.)方法用于在光标处替换和添加元素 ListIterator源码可以看我这篇文章 ListIterator
2.接口定义的方法
方法名 | 作用 |
---|
boolean hasNext() | 如果迭代器还有剩余元素,则返回true | E next() | 返回迭代器中的下一个元素 | void remove() | 从基础集合中移除此迭代器返回的最后一个元素(即移除调用next()返回的元素), 每次调用next后只能调用此方法一次 | void forEachRemaining(Consumer<? super E> action) | 对每个剩余元素执行给定操作,直到所有元素都已处理或该操作引发异常为止。 |
3.forEachRemaining的默认实现
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
Consumer是函数式接口,里面包含一个参数的无返回值方法accept()表示一个操作, Consumer的源码可以看我这篇文章 Consumer
Iterator接口源码
package java.util;
import java.util.function.Consumer;
/**
* An iterator over a collection. {@code Iterator} takes the place of
* {@link Enumeration} in the Java Collections Framework. Iterators
* differ from enumerations in two ways:
* 集合上的迭代器。Iterator 在Java集合框架中取代了Enumeration。
* 迭代器与枚举有两种不同:
*
* <ul>
* <li> Iterators allow the caller to remove elements from the
* underlying collection during the iteration with well-defined
* semantics.
* 迭代器允许调用方在迭代期间使用定义良好的语义从基础集合中删除元素。
*
* <li> Method names have been improved. 方法名称已得到改进。
* </ul>
*
* <p>This interface is a member of the
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
* Java Collections Framework</a>.
*
* @apiNote
* An {@link Enumeration} can be converted into an {@code Iterator} by
* using the {@link Enumeration#asIterator} method.
* Enumeration 可以通过使用Enumeration.asIterator()方法转换为Iterator。
*
* @param <E> the type of elements returned by this iterator
*
* @author Josh Bloch
* @see Collection
* @see ListIterator
* @see Iterable
* @since 1.2
*/
public interface Iterator<E> {
/**
* Returns {@code true} if the iteration has more elements.
* (In other words, returns {@code true} if {@link #next} would
* return an element rather than throwing an exception.)
* 如果迭代包含更多元素,则返回true。(换句话说,
* 如果next将返回元素而不是抛出异常,则返回true
*
* @return {@code true} if the iteration has more elements
*/
boolean hasNext();
/**
* Returns the next element in the iteration.
* 返回迭代器中的下一个元素。
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iteration has no more elements 如果迭代器没有更多的元素
*/
E next();
/**
* Removes from the underlying collection the last element returned
* by this iterator (optional operation). This method can be called
* only once per call to {@link #next}.
* 从基础集合中移除此迭代器返回的最后一个元素(可选操作)。
* 每次调用next只能调用此方法一次。
* <p>
* The behavior of an iterator is unspecified if the underlying collection
* is modified while the iteration is in progress in any way other than by
* calling this method, unless an overriding class has specified a
* concurrent modification policy.
* <p>
* The behavior of an iterator is unspecified if this method is called
* after a call to the {@link #forEachRemaining forEachRemaining} method.
*
* @implSpec
* The default implementation throws an instance of
* {@link UnsupportedOperationException} and performs no other action.
* 默认实现抛出UnsupportedOperationException的实例,不执行其他操作。
*
* @throws UnsupportedOperationException if the {@code remove}
* operation is not supported by this iterator
*
* @throws IllegalStateException if the {@code next} method has not
* yet been called, or the {@code remove} method has already
* been called after the last call to the {@code next}
* method
* 如果尚未调用next方法,或者在上次调用next方法之后已经调用了remove方法
*/
default void remove() {
throw new UnsupportedOperationException("remove");
}
/**
* Performs the given action for each remaining element until all elements
* have been processed or the action throws an exception. Actions are
* performed in the order of iteration, if that order is specified.
* Exceptions thrown by the action are relayed to the caller.
* 对每个剩余元素执行给定操作,直到所有元素都已处理或该操作引发异常为止。
* 如果指定了迭代顺序,则按迭代顺序执行操作。操作引发的异常将转发给调用方。
*
* <p>
* The behavior of an iterator is unspecified if the action modifies the
* collection in any way (even by calling the {@link #remove remove} method
* or other mutator methods of {@code Iterator} subtypes),
* unless an overriding class has specified a concurrent modification policy.
* 如果操作以任何方式修改集合(甚至通过调用remove方法或iterator子类型的其他mutator方法),
* 则迭代器的行为是未指定的,除非重写类指定了并发修改策略。
* <p>
* Subsequent behavior of an iterator is unspecified if the action throws an
* exception.
* 如果操作引发异常,则未指定迭代器的后续行为。
*
* @implSpec
* <p>The default implementation behaves as if:
* 默认实现的行为方式如下所示:
* <pre>{@code
* while (hasNext())
* action.accept(next());
* }</pre>
*
* @param action The action to be performed for each element 为每个元素执行的操作
* @throws NullPointerException if the specified action is null 如果指定的操作为空
* @since 1.8
*/
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}
|