简体   繁体   English

为什么在java.util.Collection上有一个方法iterator()

[英]Why is there a method iterator() on java.util.Collection

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. 为什么在接口java.util.Collection上定义了iterator()方法,它已经扩展了java.util.Iterable,它定义了这个方法。

I'm thinking some sort of backward compatability or an opportunity to write some JavaDoc on the method at the collection level. 我正在考虑某种向后兼容性或在集合级别上对方法编写一些JavaDoc的机会。

Any other ideas? 还有其他想法吗?

Backwards compatibility. 向后兼容性。 Iterable was not introducted until 1.5 with the for(Object o : iterable) construct. 直到1.5使用for(Object o:iterable)构造才引入Iterable。 Previously, all collections had to provide a means to iterate them. 以前,所有集合都必须提供迭代它们的方法。

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. 虽然javadoc很好,但很难注意到/一个方法从一个接口移动到一个超级接口。

Note the same was done with Closeable, also introduced in 1.5. 注意与Closeable相同,也在1.5中引入。

As far as I am aware there would have been no binary compatability issues with removing the method from the Collection class. 据我所知,从Collection类中删除方法没有二进制兼容性问题。

Iterable was introduced in 1.5. Iterable在1.5中引入。 Since it was part of Collection from before 1.5 that they probably just didn't remove it. 因为它是1.5之前的Collection的一部分,所以他们可能只是没有删除它。 And as the other contributor pointed out, it does have better JavaDoc. 正如另一位撰稿人指出的那样,它确实有更好的JavaDoc。

I have been doing some more investigation on this and have found that the equals() and hashcode() methods are also overwritten. 我一直在对此进行更多调查,并发现equals()和hashcode()方法也被覆盖。

Clearly the only reason for this can be to add the javadoc - maybe this is why iterator() was also overwritten. 显然,唯一的原因可能是添加javadoc - 也许这就是为什么iterator()也被覆盖了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM