方法iterator()在java.util.Collection和java.lang.Iterable中声明,它的超级接口? [英] Method iterator() declared in java.util.Collection and in java.lang.Iterable, its superinterface?
问题描述
有人可以向我解释为什么方法 Iterator< E> iterator();
在 java.util.Collection
中定义?集合已经扩展 java.lang.Iterable
;这种方法是多余的。这是为了方便吗?
Can somebody explain to me why is the method Iterator<E> iterator();
defined in java.util.Collection
? Collection already extends java.lang.Iterable
; this method is redundant. Is this for convenience?
推荐答案
集合
接口是在Java 1.2中使用Collections API引入的。然后存在迭代器
方法。但是, Iterable $直到Java 1.5才引入c $ c> interface
。 Collection
显式定义 iterator
的原因是因为它早于 Iterable
。 集合
返回 Iterator
的想法早于 Iterable $ c $的想法c>。
The Collection
interface was introduced in Java 1.2 with the Collections API. The iterator
method was present then. However, the Iterable
interface wasn't introduced until Java 1.5. The reason that Collection
explicitly defines iterator
is because it predates Iterable
. The idea of a Collection
returning an Iterator
predates the idea of Iterable
.
这篇关于方法iterator()在java.util.Collection和java.lang.Iterable中声明,它的超级接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!