java.util.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:
* 集合上的迭代器
* 在Java Collections Framework中,Iterator代替了Enumeration
* Iterator与Enumeration的不同有以下两点:
* <ul>
* <li> Iterators allow the caller to remove elements from the
* underlying collection during the iteration with well-defined
* semantics.
* Iterators允许调用者在迭代期间使用定义明确的语义从底层集合中删除元素。
* <li> Method names have been improved.
* 方法名称已得到改进。
* </ul>
*
* <p>This interface is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @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.
* 如果iteration有更多的元素那么就返回true
* (In other words, returns {@code true} if {@link #next} would
* return an element rather than throwing an exception.)
* (换句话说,如果next方法返回的是一个元素而不是抛出一个异常的话那么就返回true)
* @return {@code true} if the iteration has more elements
*/
boolean hasNext();
/**
* Returns the next element in the iteration.
* 返回iteration中的下一个元素。
* @return the next element in the iteration
* @return iteration中的下一个元素。
* @throws NoSuchElementException if the iteration has no more elements
* @抛出NoSuchElementException 如果iterations中没有更多的元素
*/
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}. 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.
* 从基础集合中移除此迭代器返回的最后一个元素(可选操作)。
* 每次调用next方法只能调用一次此方法。
* 如果在迭代进行过程中以其他方式(而不是通过调用此方法)修改了基础集合,则迭代器的行为是不确定的。
*
* @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
* 抛出UnsupportedOperationException异常如果remove方法的操作不被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
* 抛出IllegalStateException异常如果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.
* 该操作引发的异常将中继到调用方。
* @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
* 抛出NullPointerException异常如果对已制定的action为null
* @since 1.8
* java.util.function.Consumer 表示一个接受单个输入参数且不返回结果的操作。
*/
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
/**
* 判断action(Consumer)是否为空
* public static <T> T requireNonNull(T obj) {
* if (obj == null)
* throw new NullPointerException();
* return obj;
*}
*/
while (hasNext())
action.accept(next());
}
该方法和forEach方法的不同在于该方法只能对所给定的元素处理一次,下一次处理是从上一次元素处理后的位置开始。而forEach则可以多次迭代。
public static void main(String[] args){
List<String> list1 = new ArrayList<String>();
list1.add("jack");
list1.add("tom");
Iterator<String> it1 = list1.iterator();
it1.forEachRemaining(String->System.out.print(String+" "));
System.out.println();
//如果在一次处理过程中加入其他操作则会报java.util.ConcurrentModificationException异常
//list1.add("john");
it1.forEachRemaining(String->System.out.print(String+" "));
System.out.println("===========================");
List<String> list2 = new ArrayList<String>();
list2.add("jack");
list2.add("tom");
list2.forEach(String->System.out.print(String+" "));
System.out.println();
list2.add("john");
list2.forEach(String->System.out.print(String+" "));
}
}