Iterator迭代器源代码简单解析

翻译:

/*
 * Copyright (c) 1997, 2013, Oracle and/or 
  版权 1997,2013,Oracle
 *its affiliates. All rights reserved.
 及其附属公司,版权所有。
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is 
 * ORACLE 所有权/机密 
 * subject to license terms.
 * 使用过程中需服从条款。
 * */
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.  
 * {@code Iterator} 在java集合框架中替代{@link Enumeration}
 * Iterators differ from enumerations in two ways:
 * 迭代器不同与枚举类主要体现在两方面:
 *
 * <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}/../technotes/guides/collections/index.html">
 * 此接口是以下技术文档地址的解释成员之一。


 * Java Collections Framework</a>.
 * java集合框架。
 * @param <E> the type of elements returned by this iterator
 * 元素类型通过此迭代器返回。
 * @author  Josh Bloch
 * 作者:Josh Bloch,Java 集合框架创办人,Joshua Bloch 领导了很多 Java 平台特性的设计和实现,包括       * JDK 5.0 语言增强以及屡获殊荣的 Java 集合框架。2004年6月他离开了SUN公司并成为 Google 的首席 Java * 架构师。此外他还因为《Effective Java》一书获得著名的 Jolt 大奖。
 * @see Collection
 * @see ListIterator
 * @see Iterable
 * @since 1.2
 * 从jdk1.2开始存在此接口。
 */
public interface Iterator<E> {
    /**
     * Returns {@code true} if the iteration has more elements.
     * 如果迭代中有更多的元素则返回true
     * (In other words, returns {@code true} if {@link #next} would
     * return an element rather than throwing an exception.)
     * 换句话说,返回true,如果将返回元素也不要抛出异常。
     *
     * @return {@code true} if the iteration has more elements
     * 如何迭代中存在许多的元素,则返回true。
     */
    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
     * 如果迭代中不存在任何元素则抛出异常NoSuchElementException.
     */
    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}.  
     * 每次调用{@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.
     *  
     * 如果迭代正在以任何方式进行着,而底层集合被修改了, 除了调用此方法,此迭代器的行为都是不明确的。-这句很难翻译啊,哪位大佬指点一下?-
     * @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
     * 如果迭代器不支持该代码({指的是下方的remove方法})的操作,那么将抛出“不支持操作的异常”。
     * @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方法)不曾被调用,或者remove方法在next方法被调用之后才被调用,那么将抛出“非法状态异常”
     */
    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
     * 如果没有详细的操作说明,则会 抛出空指针异常 异常。
     * @since 1.8
     * 从jdk1.8开始有此代码。
     */
    default void forEachRemaining(Consumer<? super E> action) {
    /**
    泛型(Consumer<? super E> action)
    ?是E的父类
    E作为?的下限
    
    */
        Objects.requireNonNull(action);
    //**.requireNonNull**需求非零,即不是没有需求。
        while (hasNext())
            action.accept(next());
     /* 
     这个accept()是一种阻塞形式的IO和监听端口方法。
     .next()方法的作用:指针指向下一条记录,有记录(有值)返回true并把记录内容存入到对应的对象中,也就是obj.next()的obj中。如果没有返回false*/
    }
}

简化

package java.util;
import java.util.function.Consumer;

public interface Iterator<E> {
    boolean hasNext();//确认该集合是否有元素;
    E next();//获得下一个元素;
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    default void forEachRemaining(Consumer<? super E> action) {
    //如果所要判断的元素为 null, 则返回空指针异常 NullPointerException, 否则直接返回对应的对象.
        Objects.requireNonNull(action);
        while (hasNext())//当集合中还有元素;
            action.accept(next());//获取集合的下一个元素。

    }
}

总结
1.使用方法 iterator()要求容器返回一个 Iterator。第一次调用Iterator 的next()方法时,它返回序列的第一个元素。
2.使用next()获得序列中的下一个元素。
3.使用hasNext()检查序列中是否还有元素。
4.使用remove()将上一次返回的元素从迭代器中移除。

参考:https://blog.csdn.net/qq_41750725/article/details/83117297

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值