迭代器如何与集合接口联系起来
我是 Java 新手。
Java 有 Collection 接口
public interface Collection<E> extends Iterable<E>
{ // a lot of other stuff.
Iterator<E> iterator(); }
我不明白的是 Iterator Interface 是如何绑定到 Collection Interface 的?当我查看 Collection Interface 时,我发现它有一个返回 Iterator 的方法。创建 Collection 的 Iterator 时,JVM 在哪里寻找创建 IS-An Iterator 的对象?
谢谢 !
I am new to Java.
Java has Collection interface
public interface Collection<E> extends Iterable<E>
{ // a lot of other stuff.
Iterator<E> iterator(); }
What I don't understand is how does Iterator Interface is tying into Collection Interface ? When I look at the Collection Interface, I see that it has a method that returns Iterator. When Iterator for Collection is created, where does JVM looks to create an object that IS-An Iterator ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JVM 不做这件事;最终实现抽象
iterator()
方法的代码只是创建适当类的实例并返回它。例如,在ArrayList()
类中,它实现了继承Collection
的List
,有一个实现iterator()的方法
通过返回一个了解 ArrayList 内部如何实现的类的实例。这个 Iterator 类是 java.util 包私有的——通常你永远不会看到它的名字。The JVM doesn't do it; the code that ultimately implements the abstract
iterator()
method just creates an instance of an appropriate class and returns it. For example, in the classArrayList()
, which implementsList
which extendsCollection
, there is a method that implementsiterator()
by returning an instance of a class that understand howArrayList
is implemented internally. ThisIterator
class is private to thejava.util
package -- normally you'll never see its name.好吧,这都是非常高级的 Java 内容,在这里一次性解释起来会非常困难,但我会尽力而为。
背景:如果您不了解那些有趣的
事情,您应该研究一下 Java 泛型。另外,如果您还不知道,您确实需要知道什么是接口。一种真正基本的思考方式是将其视为类承诺提供的承诺功能。现在回答您的问题:上面的代码片段中有三个接口,如果您想创建自己的集合类,您将需要提供所有三个接口的实现:
第一个是
Collection
。这是一个映射到现实世界的简单概念,它实际上是对象的“集合”。我想你明白了......下一个是
Iterable
,它定义了所有集合都需要提供的单一类型的行为:遍历集合的所有元素,同时访问它们的能力通过一即“迭代”它们。但它并不止于此。正如您所指出的,Iterable 功能是由实现最后一个接口的对象提供的:Iterator:实现此接口的对象实际上知道如何遍历集合类的元素,他们向客户隐藏了实际如何完成的所有细节,并证明了一些实际执行此操作的干净简单的方法,例如
hasNext()
,它检查集合中是否还有更多内容可供访问和next()
它实际上访问了下一个东西。唷...
Ok so this is all pretty advanced java stuff and it will be pretty tough to explain in one go here but I will do my best.
BACKGROUND: If you don't know about those funny
<E>
things, you should do a bit of looking into Java Generics. Also, if you don't already, you really need to know what an interface is. One really basic way to think of it is as a promised bit of functionality a class promises to provide.Now to answer your question: There are three interfaces in the above code snippet, and if you want to create your own collection class you will need to provide implementations of all three:
The first is
Collection
. This is a simple concept that maps to the real world, it is literally a "collection" of objects. I think you get this...The next one is
Iterable
this defines a singe type of behavior that all collections need to provide: the ability to traverse all of the elements of a collection, while accessing them one by one ie "iterate" over them. But it doesn't stop there. As you pointed out theIterable
functionality is provided by objects that implement the last interface:Iterator
: objects that implement this interface, actually know how to traverse the elements of a collection class, they hide all the details of how its actually done from thier clients and proved a few clean easy methods for actually doing it likehasNext()
which checks to see if there are more things in the collection to visit andnext()
which actually visits the next thing.phew...
iterator() 方法的代码决定返回 Iterator 的哪个具体实现。
所有实现 Collection 接口的非抽象类都需要提供 iterator() 方法的实现。
The code for the iterator() method determines which concrete implementation of Iterator to return.
All non-abstract classes that implement the Collection interface are required to provide an implementation for the iterator() method.