java - 为什么 Iterable 和 Collection 接口中都存在迭代器方法?
问题描述
该Iterable
接口有以下方法:
Iterator<T> iterator();
Collection
接口 extends 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.
*/
推荐阅读
- postgresql - 通过matlab执行postgresql命令
- javascript - Javascript 对象转换
- gitlab - 如何从远程分支获取更改?
- r - 根据存储在 data.frame 中的单独字符向量,有条件地重命名列表中的列
- hyper-v - 无法在 ryzen 处理器的固件中打开 hyper-v,在 bios 中没有启用选项
- xamarin.forms - 选择器列表更新了后端但不是 xamarin 表单中的前端
- javascript - 分布式 node.js 应用程序的代码覆盖率
- python-3.x - 如何获取对象的引用链,不包括弱引用?
- javascript - Django 视图在 AJAX POST 请求后没有重定向
- javascript - React:内联样式未应用于子组件