iterator遍历集合解析
2010-10-30 15:51
603 查看
iterator模式是用于遍历集合类的标准访问方法。它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构。
首先看看java.util.Iterator接口的定义:
每一种集合类返回的Iterator具体类型可能不同,Array可能返回ArrayIterator,Set可能返回SetIterator,Tree
可能返回TreeIterator,但是它们都实现了Iterator接口,因此,客户端不关心到底是哪种Iterator,它只需要获得这个
Iterator接口即可,这就是面向对象的威力。
让我们来看看AbstracyList如何创建Iterator。首先AbstractList定义了一个内部类(inner class):
而iterator()方法的定义是:
因此客户端不知道它通过Iterator it = a.iterator();所获得的Iterator的真正类型。
现在我们关心的是这个申明为private的Itr类是如何实现遍历AbstractList的:
Itr类依靠3个int变量(还有一个隐含的AbstractList的引用)来实现遍历,cursor是下一次next()调用时元素的位置,第一次调用next()将返回索引为0的元素。lastRet记录上一次游标所在位置,因此它总是比cursor少1。
变量cursor和集合的元素个数决定hasNext():
方法next()返回的是索引为cursor的元素,然后修改cursor和lastRet的值:
expectedModCount表示期待的modCount值,用来判断在遍历过程中集合是否被修改过。AbstractList包含一个
modCount变量,它的初始值是0,当集合每被修改一次时(调用add,remove等方法),modCount加1。因此,modCount如果不
变,表示集合内容未被修改。
Itr初始化时用expectedModCount记录集合的modCount变量,此后在必要的地方它会检测modCount的值:
如果modCount与一开始记录在expectedModeCount中的值不等,说明集合内容被修改过,此时会抛出ConcurrentModificationException。
这个ConcurrentModificationException是RuntimeException,不要在客户端捕获它。如果发生此异常,说明程序代码的编写有问题,应该仔细检查代码而不是在catch中忽略它。
但是调用Iterator自身的remove()方法删除当前元素是完全没有问题的,因为在这个方法中会自动同步expectedModCount和modCount的值:
要确保遍历过程顺利完成,必须保证遍历过程中不更改集合的内容(Iterator的remove()方法除外),因此,确保遍历可靠的原则是只在一个线程中使用这个集合,或者在多线程中对遍历代码进行同步。
转自:http://gceclub.sun.com.cn/yuanchuang/week-14/iterator.html
首先看看java.util.Iterator接口的定义:
public interface Iterator { boolean hasNext(); Object next(); void remove(); }
每一种集合类返回的Iterator具体类型可能不同,Array可能返回ArrayIterator,Set可能返回SetIterator,Tree
可能返回TreeIterator,但是它们都实现了Iterator接口,因此,客户端不关心到底是哪种Iterator,它只需要获得这个
Iterator接口即可,这就是面向对象的威力。
让我们来看看AbstracyList如何创建Iterator。首先AbstractList定义了一个内部类(inner class):
private class Itr implements Iterator { ... }
而iterator()方法的定义是:
public Iterator iterator() { return new Itr(); }
因此客户端不知道它通过Iterator it = a.iterator();所获得的Iterator的真正类型。
现在我们关心的是这个申明为private的Itr类是如何实现遍历AbstractList的:
private class Itr implements Iterator { int cursor = 0; int lastRet = -1; int expectedModCount = modCount; }
Itr类依靠3个int变量(还有一个隐含的AbstractList的引用)来实现遍历,cursor是下一次next()调用时元素的位置,第一次调用next()将返回索引为0的元素。lastRet记录上一次游标所在位置,因此它总是比cursor少1。
变量cursor和集合的元素个数决定hasNext():
public boolean hasNext() { return cursor != size(); }
方法next()返回的是索引为cursor的元素,然后修改cursor和lastRet的值:
public Object next() { checkForComodification(); try { Object next = get(cursor); lastRet = cursor++; return next; } catch(IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } }
expectedModCount表示期待的modCount值,用来判断在遍历过程中集合是否被修改过。AbstractList包含一个
modCount变量,它的初始值是0,当集合每被修改一次时(调用add,remove等方法),modCount加1。因此,modCount如果不
变,表示集合内容未被修改。
Itr初始化时用expectedModCount记录集合的modCount变量,此后在必要的地方它会检测modCount的值:
final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }
如果modCount与一开始记录在expectedModeCount中的值不等,说明集合内容被修改过,此时会抛出ConcurrentModificationException。
这个ConcurrentModificationException是RuntimeException,不要在客户端捕获它。如果发生此异常,说明程序代码的编写有问题,应该仔细检查代码而不是在catch中忽略它。
但是调用Iterator自身的remove()方法删除当前元素是完全没有问题的,因为在这个方法中会自动同步expectedModCount和modCount的值:
public void remove() { ... AbstractList.this.remove(lastRet); ... // 在调用了集合的remove()方法之后重新设置了expectedModCount: expectedModCount = modCount; ... }
要确保遍历过程顺利完成,必须保证遍历过程中不更改集合的内容(Iterator的remove()方法除外),因此,确保遍历可靠的原则是只在一个线程中使用这个集合,或者在多线程中对遍历代码进行同步。
转自:http://gceclub.sun.com.cn/yuanchuang/week-14/iterator.html
相关文章推荐
- 用Iterator实现遍历集合
- Iterator 关键字,用于集合的遍历
- Iterator遍历有多级子集合的集合
- 用Iterator实现遍历集合
- 遍历map集合Iterator保存对象
- Struts标签-使用<s:iterator>迭代遍历集合中的集合
- Java Iterator遍历List集合
- 集合遍历之Iterator与foreach
- 集合中关于iterator遍历顺序的理解
- Iterator遍历有多级子集合的集合
- 集合遍历操作:Collection及Iterator接口
- Java 集合源码解析(1):Iterator
- 使用Struts2标签<s:iterator>来遍历集合
- java-Iterator(迭代器)遍历集合
- Iterator遍历有多级子集合的集合
- 使用Java8增强的Iterator遍历集合元素
- Java类集框架——Iterator和ListIterator 迭代器的使用(遍历集合)
- 用foreach和Iterator遍历集合的方法
- 如何边遍历集合边删除元素--使用Iterator中的remove()方法
- 用Iterator实现遍历集合