Java 中的 Iterator 接口有什么好处? [英] What are the benefits of the Iterator interface in Java?
问题描述
我刚刚了解了 Java 集合框架如何实现链表中的数据结构.据我了解,Iterators
是一种遍历数据结构(如列表)中的项的方法.为什么要使用这个接口?为什么方法 hasNext()
、next()
和 remove()
没有直接编码到数据结构实现本身?
I just learned about how the Java Collections Framework implements data structures in linked lists. From what I understand, Iterators
are a way of traversing through the items in a data structure such as a list. Why is this interface used? Why are the methods hasNext()
, next()
and remove()
not directly coded to the data structure implementation itself?
来自 Java 网站:链接文本
From the Java website: link text
公共接口迭代器 一个集合上的迭代器.迭代器代替枚举Java 集合框架.迭代器与枚举有两个不同之处: public interface Iterator<E> An
iterator over a collection. Iterator
takes the place of Enumeration in the
Java collections framework. Iterators
differ from enumerations in two ways:
- Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
- Method names
have been improved.
我尝试在谷歌上搜索,似乎找不到明确的答案.有人可以解释为什么 Sun 选择使用它们吗?是因为更好的设计吗?增加安全性?良好的面向对象实践?
I tried googling around and can't seem to find a definite answer. Can someone shed some light on why Sun chose to use them? Is it because of better design? Increased security? Good OO practice?
任何帮助将不胜感激.谢谢.
Any help will be greatly appreciated. Thanks.
推荐答案
为什么要使用这个接口?
Why is this interface used?
因为它支持允许客户端程序员迭代任何类型的集合的基本操作(注意:不一定是
Object
意义上的Collection
).Because it supports the basic operations that would allow a client programmer to iterate over any kind of collection (note: not necessarily a
Collection
in theObject
sense).为什么方法...不是直接的编码到数据结构实现本身?
Why are the methods... not directly coded to the data structure implementation itself?
他们是,他们只是被标记为私人,所以你不能接触他们并与他们混在一起.更具体地说:
They are, they're just marked Private so you can't reach into them and muck with them. More specifically:
- 您可以实现或子类化
Iterator
以便它执行标准操作无法执行的操作,而无需更改它迭代的实际对象. - 可以遍历的对象不需要让它们的接口与遍历方法(尤其是任何高度专业化的方法)混在一起.
- 您可以将
Iterators
分发给您希望的任意数量的客户端,并且每个客户端都可以按照自己的速度以自己的时间遍历. - Java
Iterators
特别是 java.util 包中的,如果支持它们的存储被修改而您仍然有Iterator
输出,则会抛出异常.此异常让您知道Iterator
现在可能正在返回无效对象.
- You can implement or subclass an
Iterator
such that it does something the standard ones don't do, without having to alter the actual object it iterates over. - Objects that can be traversed over don't need to have their interfaces cluttered up with traversal methods, in particular any highly specialized methods.
- You can hand out
Iterators
to however many clients you wish, and each client may traverse in their own time, at their own speed. - Java
Iterators
from the java.util package in particular will throw an exception if the storage that backs them is modified while you still have anIterator
out. This exception lets you know that theIterator
may now be returning invalid objects.
对于简单的程序,这些似乎都不值得.不过,让它们有用的复杂性很快就会出现.
For simple programs, none of this probably seems worthwhile. The kind of complexity that makes them useful will come up on you quickly, though.
这篇关于Java 中的 Iterator 接口有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 您可以实现或子类化