在Java中,Iterator的作用就是为了方便处理集合中的元素。例如获取和删除集合中的元素。

在JDK8,Iterator接口提供了如下方法:

Iterator-Java-LMLPHP

迭代器Iterator最基本的两个方法是next()和hasNext()。其中Java迭代器多了一个remove()方法。在JDK8中又新增了forEachRemaining()方法。

接下来,以ArrayList为例,看下迭代器在Java中的实现。

在ArrayList中,以内部类的方式实现,每次调用iterator()方法,都会new Itr()。

先看一下ArrayList迭代器的实现类:

Iterator-Java-LMLPHP

定义的参数如下:

 int cursor;       // 下标,默认值0;
int lastRet = -1; //最后一个元素的下标,如果没有返回-1;
int expectedModCount = modCount;//结构修改(添加、删除等操作)次数,默认值0;

定义的方法如下:

hasNext()方法:

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

当集合中不存在下一个元素,即已经遍历至集合末尾时,该方法返回false。hasNext 方法主要用于循环终止条件。

next()方法:

 public E next() {
checkForComodification();
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];
}

在执行next()方法时,会先执行checkForComodification()方法,做一个判断(检查所迭代的列表对象是否被修改过):

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

在对集合进行迭代过程中,不允许出现迭代器以外的对元素的操作,因为这样会产生安全隐患,java会抛出异常并发修改异常(ConcurrentModificationException),普通迭代器只支持在迭代过程中的删除动作。

如果在遍历ArrayList时,想添加元素和修改元素,可以调用List集合的特有迭代器ListIterator。

remove()方法:

 public void remove() {
if (lastRet < 0) throw new IllegalStateException();
checkForComodification(); try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}

Java普通迭代器中唯一支持修改结构的操作。在执行完删除操作后,会更新expectedModCount字段的值。

forEachRemaining()方法:

 public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// update once at end of iteration to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
}

forEachRemaining()是JDK8中新增的方法。forEachRemaining()使用迭代器Iterator的所有元素,并且第二次调用它将不会做任何事情,因为不再有下一个元素。

一个使用场景:获得对应集合的迭代器Iterator,然后您可以开始迭代,next()直到达到某个条件,然后使用forEachRemaining()操作该Iterator上的其余部分。

接下来,看一下刚才提到的ListIterator。

在普通的Iterator中,只能对元素进行获取(next())和删除(remove())的操作。而ListIterator提供了更多的方法:

Iterator-Java-LMLPHP

add(E e): 将指定的元素插入列表,插入位置为迭代器当前位置之前
hasNext():以正向遍历列表时,如果列表迭代器后面还有元素,则返回 true,否则返回false
hasPrevious():如果以逆向遍历列表,列表迭代器前面还有元素,则返回 true,否则返回false
next():返回列表中ListIterator指向位置后面的元素
nextIndex():返回列表中ListIterator所需位置后面元素的索引
previous():返回列表中ListIterator指向位置前面的元素
previousIndex():返回列表中ListIterator所需位置前面元素的索引

在使用特有的迭代器时,我们就能做更多的操作了。

05-12 14:45