Enumeration
接口源码
根据以下源码和注释可以知道,此接口和iterator功能是相同的,并且在源码注解中也是推荐使用iterator代替它使用,Enumeration提供的两个函数只有读的操作,只有在Vector和Hashtale有它们的实现。
/**
* 推荐使用iterator
* NOTE: The functionality of this interface is duplicated by the Iterator
* interface. In addition, Iterator adds an optional remove operation, and
* has shorter method names. New implementations should consider using
* Iterator in preference to Enumeration.
*
* @see java.util.Iterator
* @see java.io.SequenceInputStream
* @see java.util.Enumeration#nextElement()
* @see java.util.Hashtable
* @see java.util.Hashtable#elements()
* @see java.util.Hashtable#keys()
* @see java.util.Vector
* @see java.util.Vector#elements()
*
* @author Lee Boynton
* @since JDK1.0
*/
public interface Enumeration<E> {
boolean hasMoreElements();
E nextElement();
}
iterator
接口源码
从以下源码中可以知道,iterator代替了enumeration。提供了对元素的读和修改方法。
/**
* An iterator over a collection. {@code Iterator} takes the place of
* {@link Enumeration} in the Java Collections Framework. Iterators
* differ from enumerations in two ways:
* @param <E> the type of elements returned by this iterator
* @see Collection
* @see ListIterator
* @see Iterable
* @since 1.2
*/
public interface Iterator