为什么 Collections.enumeration 带有 enumeration() 方法

Why Collections.enumeration comes with enumeration() method

在Collections Framework 中,我们有IteratorListIterator 接口,用于迭代数据结构。它们为迭代提供了完整的功能。但我想知道为什么 JDK 在 Collections class.

上有一个 enumeration() 方法

谁能给我解释一下这一点?

IteratorListIterator 提供了完整的工具来遍历集合。事实上,documentation for Iterator 本质上是说 Iterator 已经取代了 Enumeration。每个集合都是一个 Iterable,因此可以使用 Iterator 进行迭代。那么为什么有一种方法 Collections.enumeration 会在该集合上生成 Enumeration

原因是 Enumeration 出现在最初的 Java 1.0 版本中,而 Iterator 直到 1.2 中添加了集合框架才引入。因此,有大量使用 Enumeration 的代码和 API。添加 Collections.enumeration 方法是为了(当时)新的基于集合的代码适应需要使用 Enumeration.

的旧代码