Java8 the Iterator interface

Functions as an interface to provide a specified type of iterator.

public interface Iterator<E> {

    // 每探测是否还有下一个元素
    boolean hasNext();

    // 返回当前迭代元素 ,迭代游标后移
    E next();

    // 删除最近一次已近迭代出出去的那个元素  
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }

    // 为每个剩余的元素执行给定的操作
    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

to sum up

Copy iterator iteration of the object is referenced, or a reference type. So if you save a collection of elements is a variable type, you can modify the original objects in the collection by iterating the elements.

for comparison loop and the iterator Iterator

ArrayList faster random access, and the for loop get () method, i.e. using a random access method, so ArrayList in, for faster cycle.
LinkedList sequential access is relatively fast, the iterator Next () method, i.e. a method of using sequential access, so LinkedList, the use iterator faster.
From the data structure point of view, it is suitable for the access order loop structure, rapid access according to the specified element index.
Iterator for accessing the chain structure, because the iterator by next () to locate. You can not access the collection order.

Guess you like

Origin www.cnblogs.com/feiqiangsheng/p/11184240.html