Java中Iterator,迭代器实现原理

“这两天正好学到了Java迭代器这块儿,一开始我以为是类似于指针的原理,但是后面的操作让我觉得和指针又差太多,查了资料看了下迭代器的原理,如下:”

迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构。迭代器通常被称为“轻量级”对象,因为创建它的代价小。

  Java中的Iterator功能比较简单,并且只能单向移动:

  (1) 使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的next()方法时,它返回序列的第一个元素。注意:iterator()方法是java.lang.Iterable接口,被Collection继承。

  (2) 使用next()获得序列中的下一个元素。

  (3) 使用hasNext()检查序列中是否还有元素。

  (4) 使用remove()将迭代器新返回的元素删除。

看一下它的工作原理:

1.public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> { // List接口实现了Collection<E>, Iterable<E> 
2.  
3.    protected AbstractList() {  
4.    }  
5.    
6.    ...  
7.  
8.    public Iterator<E> iterator() {  
9.    return new Itr();  // 这里返回一个迭代器
10.    }  
11.  
12.    private class Itr implements Iterator<E> {  // 内部类Itr实现迭代器
13.       
14.    int cursor = 0;  
15.    int lastRet = -1;  
16.    int expectedModCount = modCount;  
17.  
18.    public boolean hasNext() {  // 实现hasNext方法
19.            return cursor != size();  
20.    }  
21.  
22.    public E next() {  // 实现next方法
23.            checkForComodification();  
24.        try {  
25.        E next = get(cursor);  
26.        lastRet = cursor++;  
27.        return next;  
28.        } catch (IndexOutOfBoundsException e) {  
29.        checkForComodification();  
30.        throw new NoSuchElementException();  
31.        }  
32.    }  
33.  
34.    public void remove() {  // 实现remove方法
35.        if (lastRet == -1)  
36.        throw new IllegalStateException();  
37.            checkForComodification();  
38.  
39.        try {  
40.        AbstractList.this.remove(lastRet);  
41.        if (lastRet < cursor)  
42.            cursor--;  
43.        lastRet = -1;  
44.        expectedModCount = modCount;  
45.        } catch (IndexOutOfBoundsException e) {  
46.        throw new ConcurrentModificationException();  
47.        }  
48.    }  
49.  
50.    final void checkForComodification() {  
51.        if (modCount != expectedModCount)  
52.        throw new ConcurrentModificationException();  
53.    }  
54.    }  
55.}

  

可以看到,实现next()是通过get(cursor),然后cursor++,相当于c++里面设置一个flag标志位,因为迭代是单方面的,所以它只能进行corsor++操作,在python里cursor称为游标,

也是起到一个标志的作用。

迭代部分的代码很简单,唯一难懂的是remove操作里涉及到的expectedModCount = modCount;

在网上查到说这是集合迭代中的一种“快速失败”机制,这种机制提供迭代过程中集合的安全性。

从源代码里可以看到增删操作都会使modCount++,通过和expectedModCount的对比,迭代器可以快速的知道迭代过程中是否存在list.add()类似的操作,存在的话快速失败!

就是说在迭代的过程中不允许进行add()类似的操作,如果有就会抛出异常。

关于modCount,API解释如下:

The number of times this list has been structurally modified. 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.

也就是说,modCount记录修改此列表的次数:包括改变列表的结构,改变列表的大小,打乱列表的顺序等使正在进行迭代产生错误的结果。

Tips:仅仅设置元素的值并不是结构的修改

我们知道的是ArrayList是线程不安全的,如果在使用迭代器的过程中有其他的线程修改了List就会抛出ConcurrentModificationException,这就是Fail-Fast机制。

顺便说一下迭代器ListIter的方法:

  • ListIter<Integer> ListIter = al.listItertor(); //创建数组列表al的迭代器ListIter
  • listIter.add(new Integer(x)); //在序号为0的元素前添加一个元素x,因为创建al的迭代器的时候,游标cursor是标志的第一个元素,也就是序号为0的元素
  • listIter.hasNext(); // 原理就是判断cursor++后的值是否为空
  • listIter.set(new Integer(x)); //把游标cursor指向的数替换为x;
  • listIter.next() //cursor++
  • listIter.previous() //用来反向遍历可,先把游标cursor定位到最后,listIter = al.listitertor(al.size());

参考链接:https://www.cnblogs.com/xiongmozhou/p/10085105.html