为什么 Iterable 和 Collection 接口中都存在迭代器方法? [复制]

2024-04-11

The Iterable接口有以下方法:

 Iterator<T> iterator();

The Collection接口扩展Iterable,并且它还声明了相同的方法。

我怀疑在设计Java集合时为什么需要两次放置相同的方法声明?


一个可能的原因可能是添加的 javadoc 明确了该方法正在做什么。为了Collection it is:

/**
 * Returns an iterator over the elements in this collection.  There are no
 * guarantees concerning the order in which the elements are returned
 * (unless this collection is an instance of some class that provides a
 * guarantee).
 *
 * @return an <tt>Iterator</tt> over the elements in this collection
 */

同时为Iterable它“唯一”是:

/**
 * Returns an iterator over elements of type {@code T}.
 *
 * @return an Iterator.
 */
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 Iterable 和 Collection 接口中都存在迭代器方法? [复制] 的相关文章

随机推荐