Java Iterator接口实现代码解析

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:
 *
 * <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" rel="external nofollow" >
 * 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.
   * (In other words, returns {@code true} if {@link #next} would
   * return an element rather than throwing an exception.)
   *
   * @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}. 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.
   *
   * @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
   */
  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
   */
  default void forEachRemaining(Consumer<? super E> action) {
    Objects.requireNonNull(action);
    while (hasNext())
      action.accept(next());
  }
}

阅读笔记

1. Iterator接口与Enumeration接口的关系/Iterator接口在Java集合库中的作用

Iterator接口是Java集合框架的一部分,被用于替代原有的Enumeration接口。(“Iterator”比“Enumeration”更简短、表意更清晰、功能更多,具体的信息下面Enumeration接口的注解中说的挺清楚,且Enumeration注解中也建议编程人员改用Iterator接口)

Java类库中,集合类的基本接口是Collection接口,而Collection接口实现了Iterable接口,Iterable接口中有一个iterator()方法用于获取Iterator对象。

package java.util;

/**
 * An object that implements the Enumeration interface generates a
 * series of elements, one at a time. Successive calls to the
 * <code>nextElement</code> method return successive elements of the
 * series.
 * <p>
 * For example, to print all elements of a <tt>Vector&lt;E&gt;</tt> <i>v</i>:
 * <pre>
 *  for (Enumeration&lt;E&gt; e = v.elements(); e.hasMoreElements();)
 *    System.out.println(e.nextElement());</pre>
 * <p>
 * Methods are provided to enumerate through the elements of a
 * vector, the keys of a hashtable, and the values in a hashtable.
 * Enumerations are also used to specify the input streams to a
 * <code>SequenceInputStream</code>.
 * <p>
 * NOTE: The functionality of this interface is duplicated by the Iterator
 * interface. In addition, Iterator adds an optional remove operation, and
 * has shorter method names. New implementations should consider using
 * Iterator in preference to Enumeration.
 *
 * @see   java.util.Iterator
 * @see   java.io.SequenceInputStream
 * @see   java.util.Enumeration#nextElement()
 * @see   java.util.Hashtable
 * @see   java.util.Hashtable#elements()
 * @see   java.util.Hashtable#keys()
 * @see   java.util.Vector
 * @see   java.util.Vector#elements()
 *
 * @author Lee Boynton
 * @since  JDK1.0
 */
public interface Enumeration<E> {
  /**
   * Tests if this enumeration contains more elements.
   *
   * @return <code>true</code> if and only if this enumeration object
   *      contains at least one more element to provide;
   *     <code>false</code> otherwise.
   */
  boolean hasMoreElements();

  /**
   * Returns the next element of this enumeration if this enumeration
   * object has at least one more element to provide.
   *
   * @return   the next element of this enumeration.
   * @exception NoSuchElementException if no more elements exist.
   */
  E nextElement();
}

2.hasNext()、next()、remove()方法的关系

hasNext()方法:判断是否还有元素可以进行迭代;

next()方法:迭代元素;

remove()方法:

/**
* Remove from the underlying collection the last element returned by this iterator
*(optional operation).
* 移除当前迭代器上一次从基础集合中迭代的元素(可选操作)
*
* This method can be called only once per call to next().
* 调用remove()方法前必须先调用next()方法,调用完一次remove()方法后想要再次调用remove()方法,
* 必须先调用next()方法。
*
* The behavior of an iterator is unspecified if the underlying collection is modifyed while
* the iteration is in progress is any way other than by call this method.
* 如果在迭代进行过程中修改了基础集合,则迭代器的行为是不确定的。
*/
public static void main(String[] args) {
    Collection<String> stringCollection = new ArrayList<>();
    stringCollection.add("Hello");
    stringCollection.add("World");
    stringCollection.add("!");
    Iterator<String> stringIterator = stringCollection.iterator();

    stringIterator.next();
    stringIterator.remove();//OK
  }
public static void main(String[] args) {
    ......
    stringIterator.next();
    stringCollection.add("abc");//基本集合被改变
    stringIterator.remove();//ERROR - java.util.ConcurrentModificationException
  }
public static void main(String[] args) {
    ......
    stringIterator.next();
    stringCollection.add("abc");//基本集合被改变
    stringIterator.next();//ERROR - java.util.ConcurrentModificationException
  }
public static void main(String[] args) {
    ......
    stringIterator.next();
    stringCollection.add("abc");//基本集合改变
    stringIterator = stringCollection.iterator();//重新获取迭代器
    stringIterator.next();//OK
    stringIterator.remove();//OK
  }

三者关系:调用remove()方法前必须先调用next()方法,调用next()方法前最好先调用hasNext()方法。

3.具体实现类

AbstractList类中定义了一个实现了Iterator接口的内部类:

private class Itr implements Iterator<E> {
  /**
   * Index of element to be returned by subsequent call to next.
   */
  int cursor = 0;

  /**
   * Index of element returned by most recent call to next or
   * previous. Reset to -1 if this element is deleted by a call
   * to remove.
   */
  int lastRet = -1;

  /**
   * The modCount value that the iterator believes that the backing
   * List should have. If this expectation is violated, the iterator
   * has detected concurrent modification.
   */
  int expectedModCount = modCount;

  public boolean hasNext() {
    return cursor != size();
  }

  public E next() {
    checkForComodification();
    try {
      int i = cursor;
      E next = get(i);
      lastRet = i;//最近一次调用next()方法返回的元素的下标。
      cursor = i + 1;//下一次调用next()方法返回的元素的下标。
      return next;
    } catch (IndexOutOfBoundsException e) {
      checkForComodification();
      throw new NoSuchElementException();
    }
  }

  public void remove() {
    if (lastRet < 0)
      throw new IllegalStateException();//所以,调用remove()前必须先调用next()
    checkForComodification();

    try {
      AbstractList.this.remove(lastRet);
      if (lastRet < cursor)
        cursor--;//因为移除了一个元素
      lastRet = -1;//所以,不能连续调用两次remove()方法
      expectedModCount = modCount;
    } catch (IndexOutOfBoundsException e) {
      throw new ConcurrentModificationException();
    }
  }

  final void checkForComodification() {
    if (modCount != expectedModCount)
      throw new ConcurrentModificationException();
  }
}

看完上面的代码,我对modCount、expectedModCount变量以及checkForComodification()方法的作用比较好奇,所以尝试着去搞清楚。

先来看modeCount变量,这个变量被声明在内部类的外部:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
  /**
   * The number of times this list has been <i>structurally modified</i>.
   * Structural modifications are those that change the size of the
   * list, or otherwise perturb it in such a fashion that iterations in
   * progress may yield incorrect results.
   * 用于表示该列表发生结构性修改的次数。结构性修改是指*更改列表的大小*或*以其他
   * 方式干扰列表*,即正在进行的迭代可能会产生错误的结果。
   *
   * <p>This field is used by the iterator and list iterator implementation
   * returned by the {@code iterator} and {@code listIterator} methods.
   * If the value of this field changes unexpectedly, the iterator (or list
   * iterator) will throw a {@code ConcurrentModificationException} in
   * response to the {@code next}, {@code remove}, {@code previous},
   * {@code set} or {@code add} operations. This provides
   * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
   * the face of concurrent modification during iteration.
   * 设计者认为,与其因为基本集合被并发修改从而使迭代产生不确定行为,不如尽早给出错误。
   *
   * <p><b>Use of this field by subclasses is optional.</b> If a subclass
   * wishes to provide fail-fast iterators (and list iterators), then it
   * merely has to increment this field in its {@code add(int, E)} and
   * {@code remove(int)} methods (and any other methods that it overrides
   * that result in structural modifications to the list). A single call to
   * {@code add(int, E)} or {@code remove(int)} must add no more than
   * one to this field, or the iterators (and list iterators) will throw
   * bogus {@code ConcurrentModificationExceptions}. If an implementation
   * does not wish to provide fail-fast iterators, this field may be
   * ignored.
   * 是否使用应需求决定。
   */
  protected transient int modCount = 0;
}

看完上面的源码注解,已经大概能够知道modCount、expectedModCount以及checkForComodification()的作用了。

假如把基础集合当作一个银行账号,基础集合中的元素表示存款。那么modCount就相当于银行为每个账号做的消费记录,expectedModCount就相当于是账号持有人自己做的一份消费记录,一般银行和账号持有人自己做的消费记录都不会出错。

final void checkForComodification() {
  if (modCount != expectedModCount)
    throw new ConcurrentModificationException();
}

一旦银行那边的消费记录和自己手里的那份消费记录对不上,肯定是账号被盗用了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Java使用Iterator迭代器遍历集合数据的方法小结

    本文实例讲述了Java使用Iterator迭代器遍历集合数据的方法.分享给大家供大家参考,具体如下: 1.使用迭代器遍历ArrayList集合 package com.jredu.oopch07; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Ch05 { public static void main(String[] args) { // TODO Auto

  • 详解Java中Iterable与Iterator用法

    在Java中,我们可以对List集合进行如下几种方式的遍历: List<Integer> list = new ArrayList<>(); list.add(5); list.add(23); list.add(42); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + ","); } Iterator it = list.iterator(); while (i

  • java中Iterator和ListIterator实例详解

    Iterator和ListIterator的作用范围以及关系: (1) Iterator可以用于迭接口List的实现ArrayList,LinkedList以及Map等. (2) ListIterator顾名思义,就是用于迭代List实现ArrayList,LinkedList. (3) 从源码或API文档中可以看出,Iterator为ListIterator的父类. public interface ListIterator<E> extends Iterator<E> { //

  • Java集合框架中迭代器Iterator解析

    Java里面的数组数据可以通过索引来获取,那么对象呢?也是通过索引吗?今天我们就来分析一下Java集合中获取集合对象的方法迭代-Iterator. 本篇文章主要分析一下Java集合框架中的迭代器部分,Iterator,该源码分析基于JDK1.8,分析工具,AndroidStudio,文章分析不足之处,还请指正! 一.简介 我们常常使用 JDK 提供的迭代接口进行 Java 集合的迭代. Iterator iterator = list.iterator(); while(iterator.has

  • 浅谈java Iterator.remove()方法的用法(详解)

    实例如下: @Test public void tt(){ List<String> list = new ArrayList<String>(); list.add( "0" ); list.add( "1" ); list.add( "2" ); list.add( "3" ); list.add( "4" ); list.add( "5" ); list.a

  • Java集合使用 Iterator 删除元素

    这篇文章主要介绍了Java集合使用 Iterator 删除元素,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 针对常见的数据集合,比如 ArrayList 列表,对其进行遍历,删除其中符合条件的某个元素,使用 iterator 迭代器进行迭代,代码如下: public class PracticeController { public static void main(String[] args) { List<String> list =

  • Java如何使用Iterator迭代器删除集合重复选项

    这篇文章主要介绍了Java如何使用Iterator迭代器删除集合重复选项,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 结合迭代器Iterator删除集合中重复的选项,这种方法有一个缺点就是最终的结果不一定是有序的. public class test { public static void main(String[] args) { // 创建一个集合 ArrayList list = new ArrayList<>(); // 往集合中

  • Java Iterator接口实现代码解析

    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 Iterator接口遍历单列集合迭代器原理详解

    这篇文章主要介绍了Java Iterator接口遍历单列集合迭代器原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Iterator接口概述 在程序开发中,经常需要遍历集合中的所有元素.针对这种需求,JDK专门提供了一个接口java.util.Iterator . Iterator 接口也是Java集合中的一员,但它与 Collection . Map 接口有所不同,Collection 接口与 Map 接口主要用于存储元素,而 Iter

  • JAVA Iterator接口与增强for循环的实现

    1 Iterator迭代器 1.1 Iterator接口 java.util.Iterator接口也是Java集合中的一员: Iterator主要用于迭代访问(即遍历)Collection中的元素,因此Iterator对象也被称为迭代器. public Iterator iterator(): 获取集合对应的迭代器,用来遍历集合中的元素. 迭代:Collection集合元素的通用获取方式.在取元素之前先判断集合中有没有元素.如果有,就把这个元素取出来:继续判断,如果还有就再取出来:就这样一直把集

  • 使用递归算法结合数据库解析成Java树形结构的代码解析

    1.准备表结构及对应的表数据 a.表结构: create table TB_TREE ( CID NUMBER not null, CNAME VARCHAR2(50), PID NUMBER //父节点 ) b.表数据: insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0); insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1); insert into tb_tree

  • java Iterator接口和LIstIterator接口分析

    java  Iterator接口和LIstIterator接口分析 目录 1.Iterator接口 2.ListIterator 3.Iterator和ListIterator的区别 正文 在继续看ArrayList源码之前,先了解Iterator接口和ListIterator接口,下篇文章详细讲解ArrayList是如何实现它们的. 我们知道,接口只是一种规范,当继承接口并实现其中的方法时,要遵循接口对方法的说明. 1.Iterator接口 Iterator接口取代了Java集合框架中的Enu

  • Java Classloader机制用法代码解析

    做Java开发,对于ClassLoader的机制是必须要熟悉的基础知识,本文针对Java ClassLoader的机制做一个简要的总结.因为不同的JVM的实现不同,本文所描述的内容均只限于Hotspot Jvm. 本文将会从JDK默认的提供的ClassLoader,双亲委托模型,如何自定义ClassLoader以及Java中打破双亲委托机制的场景四个方面入手去讨论和总结一下. JDK默认ClassLoader JDK 默认提供了如下几种ClassLoader Bootstrp loader Bo

  • Java中LinkedList原理代码解析

    本文研究的主要是Java中LinkedList原理的相关内容,具体介绍如下. 一句话概括,Java中的LinkedList其实就是使用双向链表,LinkedList的基本操作就是对双向链表的操作. 上面可以清晰的看出,链表中每个元素对应一个节点,节点里面包含三部分,一个是前一个节点的引用,一个是元素内容,一个是后一个节点的引用. 向链表中添加元素的过程就是在链表尾部追加一个节点 void linkLast(E e) { final Node<E> l = last; final Node<

  • java  Iterator接口和LIstIterator接口分析

    java  Iterator接口和LIstIterator接口分析 目录 1.Iterator接口 2.ListIterator 3.Iterator和ListIterator的区别 正文 在继续看ArrayList源码之前,先了解Iterator接口和ListIterator接口,下篇文章详细讲解ArrayList是如何实现它们的. 我们知道,接口只是一种规范,当继承接口并实现其中的方法时,要遵循接口对方法的说明. 1.Iterator接口 Iterator接口取代了Java集合框架中的Enu

  • Java时区转换实例代码解析

    一.时区的说明 地球表面按经线从东到西,被划成一个个区域,规定相邻区域的时间相差1小时.在同一区域内的东端和西端的人看到太阳升起的时间最多相差不过1小时.当人们跨过一个区域,就将自己的时钟校正1小时(向西减1小时,向东加1小时),跨过几个区域就加或减几小时 ,所以同一时刻在不同时区表示的时间是不一样的. 二.时间的表示 我们平时表示时间时通常是以一个格式化的字符串来表示一个时间,例如"2019-11-5 20:05"这个字符串表示的是2019年11月5日20点05分.但这里有一个隐含的

  • 使用Java方法配置Spring代码解析

    使用Java的方式配置Spring 我们现在要完全不使用Spring的xml配置,全权使用Java来配置Spring! JavaConfig是Spring的一个子项目,在Spring4之后,他成为了一个核心功能. 实体类: public class User { private String name; public String getName() { return name; } @Value("huba") //属性注入值 public void setName(String n

随机推荐