0

In Collections Framework, we have the Iterator and ListIterator interfaces, which are used for iterating data structures. They provide full functionality for iteration. But I wonder that why the JDK has an enumeration() method on the Collections class.

Can anyone explain this point to me?

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Quan Nguyen
  • 698
  • 1
  • 9
  • 19
  • Reopened because, while the [other question](http://stackoverflow.com/questions/20707657/confusion-about-collections-enumeration-and-iterator-in-java) talks a lot about `Collections` and `Enumeration`, it doesn't actually answer the specific question about the reason for the `Collection.enumeration` method. – Stuart Marks Aug 15 '15 at 18:39

1 Answers1

2

The Iterator and ListIterator provide complete facilities to iterate over collections. Indeed, the documentation for Iterator essentially says that Iterator has superseded Enumeration. Every collection is an Iterable and therefore can be iterated using an Iterator. Why then is there a method Collections.enumeration that produces an Enumeration over that collection?

The reason is that Enumeration was present in the original Java 1.0 release, and Iterator was not introduced until the collections framework was added in 1.2. Thus, there was a body of code and APIs that used Enumeration. The Collections.enumeration method was added to in order for (then) new collections-based code to adapt to old code that required the use of Enumeration.

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259