Iterator
迭代器是一种遍历集合元素的一种模式,是访问集合中元素的一种方法。
- 
Iterator源码
 
public interface Iterator<E> {
    /**
     * 是否有下一个元素
     */
    boolean hasNext();
    /**
     * 下一个元素
     */
    E next();
    /**
     * 删除(不支持)
     */
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    /**
     * 遍历元素,并在action中对元素进行处理
     */
    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}
- 
Iterator源码
 
public interface ListIterator<E> extends Iterator<E> {
    // 查询操作
    /**
     * 是否有下一个
     */
    boolean hasNext();
    /**
     * 下一个元素
     */
    E next();
    /**
     * 是否有上一个
     */
    boolean hasPrevious();
    /**
     * 上一个元素
     */
    E previous();
    /**
     * 下一个元素的索引
     */
    int nextIndex();
    /**
     * 上一个元素的索引
     */
    int previousIndex();
    // 修改操作
    /**
     * 移除当前元素
     */
    void remove();
    /**
     * 设置当前元素为e
     */
    void set(E e);
    /**
     * 插入元素
     */
    void add(E e);
}












网友评论