为什么 java.util.Collection 上有一个方法 iterator()
当接口 java.util.Collection 已经扩展了定义了此方法的 java.util.Iterable 时,为什么还要在接口 java.util.Collection 上定义该方法 iterator() 呢?
我正在考虑某种向后兼容性,或者有机会在集合级别的方法上编写一些 JavaDoc。
还有其他想法吗?
Why is there the method iterator() defined on the interface java.util.Collection when it already extends java.util.Iterable which has this very method defined.
I'm thinking some sort of backward compatability or an opportunity to write some JavaDoc on the method at the collection level.
Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
向后兼容性。 Iterable 直到 1.5 才通过 for(Object o : iterable) 结构引入。以前,所有集合都必须提供迭代它们的方法。
Backwards compatibility. Iterable was not introducted until 1.5 with the for(Object o : iterable) construct. Previously, all collections had to provide a means to iterate them.
我怀疑这只是为了避免从文档的角度删除方法。尽管 javadoc 很好,但很难注意到/理解一种方法从一个接口移动到超级接口。
请注意,Closeable 也做了同样的事情,也在 1.5 中引入了。
据我所知,从 Collection 类中删除该方法不会出现二进制兼容性问题。
I suspect it was just to avoid the appearance of removing a method from a documentation point of view. Although javadoc is nice it would be difficult to notice/appreicate a method being moved from one interface to a super-interface.
Note the same was done with Closeable, also introduced in 1.5.
As far as I am aware there would have been no binary compatability issues with removing the method from the Collection class.
Iterable 是在 1.5 中引入的。由于它是 1.5 之前的 Collection 的一部分,因此他们可能只是没有将其删除。正如另一位贡献者指出的那样,它确实有更好的 JavaDoc。
Iterable was introduced in 1.5. Since it was part of Collection from before 1.5 that they probably just didn't remove it. And as the other contributor pointed out, it does have better JavaDoc.
我对此进行了更多调查,发现 equals() 和 hashcode() 方法也被覆盖。
显然,唯一的原因可能是添加 javadoc - 也许这就是 iterator() 也被覆盖的原因。
I have been doing some more investigation on this and have found that the equals() and hashcode() methods are also overwritten.
Clearly the only reason for this can be to add the javadoc - maybe this is why iterator() was also overwritten.