方法iterator()在java.util.Collection和java.lang.Iterable中声明,它的超级接口? [英] Method iterator() declared in java.util.Collection and in java.lang.Iterable, its superinterface?

查看:478
本文介绍了方法iterator()在java.util.Collection和java.lang.Iterable中声明,它的超级接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么方法 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 interface Collection 显式定义 iterator 的原因是因为它早于 Iterable 集合返回 Iterator 的想法早于 Iterable

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆