java - 为什么在 Iterable 和 Collection 接口(interface)中都存在迭代器方法?

标签 java collections iterable

<分区>

Iterable 接口(interface)有如下方法:

 Iterator<T> iterator();

Collection 接口(interface)扩展了Iterable,它也声明了相同的方法。

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

最佳答案

一个可能的原因可能是添加的 javadoc 明确了该方法的作用。对于 Collection,它是:

/**
 * 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.
 */

关于java - 为什么在 Iterable 和 Collection 接口(interface)中都存在迭代器方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51232366/

相关文章:

java - Android MapController 导致我的应用程序崩溃

java - 为什么我无法从 PriorityQueue 中删除通过 peek() 获取的元素?

java - 为什么错了?关于 Java 8 流式处理

java - 将泛型类型类列表转换为对象列表

java - 尽早终止 Iterable.forEach

java - for-each 不适用于表达式类型所需的数组或 java.lang.iterable

python - 检查类是否可迭代

java - ScatterChart 使用 LineMarker 作为 ScatterStyle 而不是仅使用 Marker

java - 如何处理 selenium 脚本来验证使用 selenium webdriver 需要 20 分钟的报告的完成情况?

java - 并发修改异常