Java中的Iterator接口有什么好处?

2022-01-10 00:00:00 oop iterator collections interface 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

公共接口迭代器

An集合上的迭代器.迭代器代替枚举中的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 选择使用它们的原因吗?是因为更好的设计吗?提高安全性?好的 OO 实践?

      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 the Object 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.util 包中的
      • Java Iterators 如果支持它们的存储在您仍然有 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 an Iterator out. This exception lets you know that the Iterator 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.

相关文章