JDK源码阅读之Iterator迭代器接口
迭代器这个名词对于熟悉Java的人来说绝对不陌生。我们常常使用JDK提供的迭代接口进行Collection的遍历:
List list =new ArrayList<String>();
list.add("aa");
list.add("bb");
list.add("cc");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String str = (String) iterator.next();
System.out.println(str);
}
//执行前list为aa bb cc
iterator.remove();
//执行前list为aa bb
而这就是关于迭代器模式应用很好的例子。
这也是迭代器最常用的方法。
源代码
package java.util;
/**
*
* @author Josh Bloch
* @see Collection
* @see ListIterator
* @see Iterable
* @since 1.2
*/
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}