# 《设计模式就该这样学》之手写自定义迭代器，秒懂迭代器底层原理

## 1　手写自定义的集合迭代器

``````public class Course {
private String name;

public Course(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
``````

``````public interface Iterator {

E next();

boolean hasNext();

}
``````

``````public interface CourseAggregate {

void remove(Course course);

Iterator iterator();
}
``````

``````public class IteratorImpl implements Iterator {

private List list;
private int cursor;
private E element;
public IteratorImpl(List list){
this.list = list;
}

public E next() {
System.out.print("当前位置" + cursor + ": ");
element = list.get(cursor);
cursor ++;
return element;
}

public boolean hasNext(){
if(cursor > list.size() - 1){
return false;
}
return true;
}
}
``````

``````public class CourseAggregateImpl implements CourseAggregate {

private List courseList;

public CourseAggregateImpl() {
this.courseList = new ArrayList();
}

}

public void remove(Course course) {
courseList.remove(course);
}

public Iterator iterator() {
return new IteratorImpl(courseList);
}
}
``````

``````public static void main(String[] args) {
Course java = new Course("Java架构");
Course javaBase = new Course("Java入门");
Course design = new Course("Java设计模式精讲");
Course ai = new Course("人工智能");

CourseAggregate courseAggregate = new CourseAggregateImpl();

System.out.println("-----课程列表-----");
printCourses(courseAggregate);

courseAggregate.remove(ai);

System.out.println("-----删除操作之后的课程列表-----");
printCourses(courseAggregate);
}

public static void printCourses(CourseAggregate courseAggregate){
Iterator iterator = courseAggregate.iterator();
while(!iterator.hasNext()){
Course course = iterator.next();
System.out.println("《" + course.getName() + "》");
}
}
``````

## 2　迭代器模式在JDK源码中的应用

``````public interface Iterator {
boolean hasNext();

E next();

default void remove() {
throw new UnsupportedOperationException("remove");
}
default void forEachRemaining(Consumer action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}
``````

``````public class ArrayList extends AbstractList
implements List, RandomAccess, Cloneable, java.io.Serializable {
...
private class Itr implements Iterator {
int cursor;       // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;

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

@SuppressWarnings("unchecked")
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];
}
...
}
...
}
``````

``````private class ListItr extends Itr implements ListIterator {
ListItr(int index) {
super();
cursor = index;
}

public boolean hasPrevious() {
return cursor != 0;
}

public int nextIndex() {
return cursor;
}

public int previousIndex() {
return cursor - 1;
}
...
}
``````

## 3　迭代器模式在MyBatis源码中的应用

``````public class DefaultCursor implements Cursor {

...

private final CursorIterator cursorIterator = new CursorIterator();
...
}
``````

爆文阅读

95后阿里P7晒出工资单：狠补了这个，真香...

《设计模式就该这样学》之使用桥接模式设计复杂的消息系统

《设计模式就该这样学》之趣谈装饰器模式，让你一辈子不会忘

《设计模式就该这样学》之使用策略模式重构电商折扣和支付场景

《设计模式就该这样学》之委派模式详解