JDK1.8源码笔记(10) Iterator&ListIterator&Iterable

Iterator

前言

An iterator over a collection.
注意限定词,“collection”。

{@code Iterator} takes the place of {@link Enumeration} in the Java Collections Framework.
Iterator是用来取代Enumeration在Collections Framework中的位置的。

其实可以顺便看一下Enumeration中的内容,可以看到只定义了两个方法,一个是hasMoreElements,另一个是nextElement。

Iterators differ from enumerations in two ways:
Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
Iterators允许调用者在iteration过程中移除下层collection中的elements。
Method names have been improved.
方法名称得到优化。

成员变量&成员方法

boolean hasNext();
Returns {@code true} if the iteration has more elements.
如果iteration还有element的话,返回true。
(In other words, returns {@code true} if {@link #next} would return an element rather than throwing an exception.)
换言之,返回true的情况就是next方法返回一个element而不是抛出异常的情况。

E next();
Returns the next element in the iteration.
@throws NoSuchElementException if the iteration has no more elements

default void remove() {
    throw new UnsupportedOperationException("remove");
}

* Removes from the underlying collection the last element returned
* by this iterator (optional operation).  This method can be called
* only once per call to {@link #next}.  The behavior of an iterator
* is unspecified if the underlying collection is modified while the
* iteration is in progress in any way other than by calling this
* method.
移除底层collection通过iterator返回的最后一个element。对于每一个next,remove只能调用一次。另外如果迭代过程中底层collection被修改,则该方法会出现不确定性。

ListIterator

前言

* An iterator for lists that allows the programmer
* to traverse the list in either direction, modify
* the list during iteration, and obtain the iterator's
* current position in the list.
功能说得很详细,能看出是一个基于list特性定制的一个iterator。

* A {@code ListIterator}
* has no current element; its <I>cursor position</I> always
* lies between the element that would be returned by a call
* to {@code previous()} and the element that would be
* returned by a call to {@code next()}.
ListIterator没有当前element这个概念。cursor所处的位置总是处于previous方法的返回值和next方法的返回值中间的位置。

* An iterator for a list of length {@code n} has {@code n+1} possible
* cursor positions, as illustrated by the carets ({@code ^}) below:
*                      Element(0)   Element(1)   Element(2)   ... Element(n-1)
* cursor positions:  ^            ^            ^            ^                  ^
一个有长度为n的list,有n+1个cursor的位置。

* Note that the {@link #remove} and {@link #set(Object)} methods are
* <i>not</i> defined in terms of the cursor position;  they are defined to
* operate on the last element returned by a call to {@link #next} or
* {@link #previous()}.
remove方法和set方法并非基于cursor的位置定义的,他们操作得是next或previous方法返回的最后一个element。

成员变量&成员方法

boolean hasNext();
这个方法在Iterator中定义得也有。
判断时候还存在下一个可以供next方法返回。

E next();
Returns the next element in the list and advances the cursor position.
这个方法在Iterator中定义得也有。
返回下一个或者抛出异常。返回完之后cursor也要移动。

boolean hasPrevious();
同hasNext。

E previous();
同next。

int nextIndex();

int previousIndex();

void remove();
这个方法在Iterator中定义得也有。
* Removes from the list the last element that was returned by {@link
* #next} or {@link #previous} (optional operation).  This call can
* only be made once per call to {@code next} or {@code previous}.
* It can be made only if {@link #add} has not been
* called after the last call to {@code next} or {@code previous}.

void set(E e);
* Replaces the last element returned by {@link #next} or
* {@link #previous} with the specified element (optional operation).
* This call can be made only if neither {@link #remove} nor {@link
* #add} have been called after the last call to {@code next} or
* {@code previous}.


void add(E e);
* Inserts the specified element into the list (optional operation).
* The element is inserted immediately before the element that
* would be returned by {@link #next}, if any, and after the element
* that would be returned by {@link #previous}, if any.  (If the
* list contains no elements, the new element becomes the sole element
* on the list.)  The new element is inserted before the implicit
* cursor: a subsequent call to {@code next} would be unaffected, and a
* subsequent call to {@code previous} would return the new element.
* (This call increases by one the value that would be returned by a
* call to {@code nextIndex} or {@code previousIndex}.)

总得来说,remove、add和set都不一定要实现,并且有一定的限制,互相最好不要掺着用。

Iterable

前言

* Implementing this interface allows an object to be the target of
* the "for-each loop" statement.
这还有个接口在蹦跶,是Collection实现的,之前都没注意。
看起来应该是为了for( : )服务的。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值