Java - Iterator 迭代器

280 阅读8分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第12天,点击查看活动详情

Iterator 接口

  • 在程序开发中,经常需要遍历集合中的所有元素

  • JDK 专门提供了一个接口java.util. Iterator

  • Iterator 接口也是 Java 集合中的一员

  • Collection接口与Map接口主要用于存储元素

  • Iterator 主要用于迭代访问(即遍历)Collection中的元素,因此 Iterator 对象也被称为迭代器

遍历

想要遍历 Collection 集合,那么就要获取该集合迭代器完成迭代操作

public Iterator Iterator (): 获取集合对应的迭代器,用来遍历集合中的元素的

迭代

即 Collection 集合元素的通用获取方式

在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续在判断,如果还有就再取出来,一直把集合中的所有元素全部取出

Iterator 接口的常用方法

  • public E next():返回迭代的下一个元素
  • public boolean hasNext():如果仍有元素可以迭代,则返回 true
public class IteratorDemo {
  	public static void main(String[] args) {
        // 使用多态方式 创建对象
        Collection<String> coll = new ArrayList<String>();

        // 添加元素到集合
        coll.add("串串星人");
        coll.add("吐槽星人");
        coll.add("汪星人");
        //遍历
        //使用迭代器 遍历   每个集合对象都有自己的迭代器
        Iterator<String> it = coll.iterator();
        //  泛型指的是 迭代出 元素的数据类型
        while(it.hasNext()){ //判断是否有迭代元素
            String s = it.next();//获取迭代出的元素
            System.out.println(s);
        }
  	}
}

tips::在进行集合元素取出时,如果集合中已经没有元素了,还继续使用迭代器的next方法,将会发生java.util.NoSuchElementException没有集合元素的错误

迭代器的实现原理

在之前案例已经完成了 Iterator 遍历集合的整个过程当遍历集合时

  1. 首先通过调用集合的 Iterator () 方法获得迭代器对象
  2. 然后使用 hashNext() 方法判断集合中是否存在下一个元素
  3. 如果存在,则调用 next() 方法将元素取出,否则说明已到达了集合末尾,停止遍历元素

原理图

Iterator 迭代器对象在遍历集合时,内部采用指针的方式来跟踪集合中的元素

  1. 在调用 Iterator 的 next 方法之前,迭代器的索引位于第一个元素之前,指向第一个元素

  2. 当第一次调用迭代器的 next 方法时,返回第一个元素,然后迭代器的索引会向后移动一位,指向第二个元素

  3. 当再次调用 next 方法时,返回第二个元素,然后迭代器的索引会再向后移动一位,指向第三个元素

  4. 依此类推,直到 hasNext 方法返回 false,表示到达了集合的末尾,终止对元素的遍历

使用 Iterator 迭代器删除元素

void remove() ;

那么,既然 Collection 已经有 remove(xx) 方法了,为什么 Iterator 迭代器还要提供删除方法呢?

因为 Collection 的 remove 方法,无法根据条件删除

例如:要删除以下集合元素中的偶数

	@Test
	public void test02(){
		Collection<Integer> coll = new ArrayList<>();
		coll.add(1);
		coll.add(2);
		coll.add(3);
		coll.add(4);
		
//		coll.remove(?)//无法编写
		
		Iterator<Integer> iterator = coll.iterator();
		while(iterator.hasNext()){
			Integer element = iterator.next();
			if(element%2 == 0){
//				coll.remove(element);//错误的
				iterator.remove();
			}
		}
		System.out.println(coll);
	}

注意:不要在使用 Iterator 迭代器进行迭代时,调用 Collection 的 remove(xx) 方法,否则会报异常java.util.ConcurrentModificationException,或出现不确定行为

增强 for

增强 for 循环(也称 for each 循环)是 JDK1.5 以后出来的一个高级 for 循环,专门用来遍历数组和集合的

格式

for(元素的数据类型  变量 : Collection集合or数组){ 
  	//写操作代码
}

练习1:遍历数组

通常只进行遍历元素,不要在遍历的过程中对数组元素进行修改

public class NBForDemo1 {
    public static void main(String[] args) {
		int[] arr = {3,5,6,87};
       	//使用增强for遍历数组
		for(int a : arr){//a代表数组中的每个元素
			System.out.println(a);
		}
	}
}

练习2:遍历集合

通常只进行遍历元素,不要在遍历的过程中对集合元素进行增加、删除、替换操作

public class NBFor {
    public static void main(String[] args) {        
    	Collection<String> coll = new ArrayList<String>();
    	coll.add("小河神");
    	coll.add("老河神");
    	coll.add("神婆");
    	//使用增强for遍历
    	for(String s :coll){//接收变量s代表 代表被遍历到的集合元素
    		System.out.println(s);
    	}
	}
}

java.lang.Iterable接口

  • java.lang.Iterable 接口,实现这个接口允许对象成为 foreach 语句的目标

  • Java 5 时 Collection 接口继承了java.lang.Iterable接口,因此 Collection 系列的集合就可以直接使用 foreach 循环遍历

java.lang.Iterable 接口的抽象方法

public Iterator Iterator ():获取对应的迭代器,用来遍历数组或集合中的元素的

自定义某容器类型

实现 java.lang.Iterable 接口,发现就可以使用 foreach 进行迭代

import java.util.Iterator;

public class TestMyArrayList {
	public static void main(String[] args) {
		MyArrayList<String> my = new MyArrayList<>();
		for(String obj : my) {
			System.out.println(obj);
		}
	}
}
class MyArrayList<T> implements Iterable<T>{

	@Override
	public Iterator<T> iterator() {
		return null;
	}
	
}

foreach 本质上就是使用 Iterator 迭代器进行遍历的

在如下代码的 for(Student student : coll) 这行打断点,然后使用单步调试进入源码,发现 foreach 本质上是调用集合的 Iterator () 方法,返回一个迭代器进行迭代的

import java.util.ArrayList;
import java.util.Collection;

public class TestForeach {
	public static void main(String[] args) {
		Collection<String> coll = new ArrayList<>();
		coll.add("陈琦");
		coll.add("李晨");
		coll.add("邓超");
		coll.add("黄晓明");
		
		//调用ArrayList里面的Iterator iterator()
		for (String str : coll) {
			System.out.println(str);
		}
	}
}

所以也不要在 foreach 遍历的过程使用Collection的remove()方法否则,要么报异常java.util.ConcurrentModificationException,要么行为不确定

Java 中 modCount 的用法,快速失败(fail-fast)机制

当使用 foreach 或 Iterator 迭代器遍历集合时,同时调用迭代器自身以外的方法修改了集合的结构,例如调用集合的add和 remove 方法时,就会报 ConcurrentModificationException

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestForeach {
	public static void main(String[] args) {
		Collection<String> list = new ArrayList<>();
		list.add("hello");
		list.add("java");
		list.add("atguigu");
		list.add("world");
		
		Iterator<String> iterator = list.iterator();
		while(iterator.hasNext()){
			list.remove(iterator.next());
		}
	}
}

如果在 Iterator 、List Iterator 迭代器创建后的任意时间从结构上修改了集合(通过迭代器自身的 remove 或 add 方法之外的任何其他方式),则迭代器将抛出 ConcurrentModificationException因此,面对并发的修改,迭代器很快就完全失败,而不是冒着在将来不确定的时间任意发生不确定行为的风险

这样设计是因为,迭代器代表集合中某个元素的位置,内部会存储某些能够代表该位置的信息当集合发生改变时,该信息的含义可能会发生变化,这时操作迭代器就可能会造成不可预料的事情因此,果断抛异常阻止,是最好的方法这就是 Iterator 迭代器的快速失败(fail-fast)机制

注意,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何坚决的保证快速失败迭代器尽最大努力抛出 ConcurrentModificationException因此,编写依赖于此异常的程序的方式是错误的,正确做法是:迭代器的快速失败行为应该仅用于检测 bug

例如:

	@Test
	public void test02() {
		ArrayList<String> list = new ArrayList<>();
		list.add("hello");
		list.add("java");
		list.add("atguigu");
		list.add("world");
		
        //以下代码没有发生ConcurrentModificationException异常
		Iterator<String> iterator = list.iterator();
		while(iterator.hasNext()){
			String str = iterator.next();
			
			if("atguigu".equals(str)){
				list.remove(str);
			}
		}
	}

那么如何实现快速失败(fail-fast)机制的呢?

  • 在 ArrayList 等集合类中都有一个 modCount 变量它用来记录集合的结构被修改的次数
  • 当给集合添加和删除操作时,会导致 modCount ++
  • 然后当用 Iterator 迭代器遍历集合时,创建集合迭代器的对象时,用一个变量记录当前集合的 modCount 例如:int expected modCount = modCount ;,并且在迭代器每次next()迭代元素时,都要检查 expected modCount != modCount ,如果不相等了,那么说明你调用了 Iterator 迭代器以外的Collection的add,remove等方法,修改了集合的结构,使得 modCount ++,值变了,就会抛出ConcurrentModificationException

下面以 AbstractList 和 ArrayList.Itr 迭代器为例进行源码分析

AbstractList 类中声明了 modCount 变量:

    /**
     * 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 是这个list被结构性修改的次数结构性修改是指:改变list的size大小,或者,以其他方式改变他导致正在进行迭代时出现错误的结果

这个字段用于迭代器和列表迭代器的实现类中,由迭代器和列表迭代器方法返回如果这个值被意外改变,这个迭代器将会抛出 ConcurrentModificationException的异常来响应:next,remove,previous,set,add 这些操作在迭代过程中,他提供了fail-fast行为而不是不确定行为来处理并发修改

子类使用这个字段是可选的,如果子类希望提供fail-fast迭代器,它仅仅需要在add(int, E),remove(int)方法(或者它重写的其他任何会结构性修改这个列表的方法)中添加这个字段调用一次add(int,E)或者remove(int)方法时必须且仅仅给这个字段加1,否则迭代器会抛出伪装的ConcurrentModificationExceptions错误如果一个实现类不希望提供fail-fast迭代器,则可以忽略这个字段

Arraylist 的 Itr 迭代器:

   private class Itr implements Iterator<E> {
        int cursor;      
        int lastRet = -1; 
        int expectedModCount = modCount;//在创建迭代器时,expectedModCount初始化为当前集合的modCount的值

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

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();//校验expectedModCount与modCount是否相等
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }
       	final void checkForComodification() {
            if (modCount != expectedModCount)//校验expectedModCount与modCount是否相等
                throw new ConcurrentModificationException();//不相等,抛异常
        }
}