<分区>
Iterable
接口(interface)有如下方法:
Iterator<T> iterator();
Collection
接口(interface)扩展了Iterable
,它也声明了相同的方法。
我很怀疑在设计 Java 集合时为什么需要将相同的方法声明两次?
<分区>
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/