5

I have an Enumeration object and I want to create a Collection object containing the items of the enumeration.

Is there any Java function to do this without manually iterating over the enumeration? Something like the reverse of the Collections.enumeration method?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329

2 Answers2

10

In fact, there is Collections.list(enumeration)

(There is also EnumerationUtils.toList(enumeration) from commons-collections.)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
2

There's nothing in the standard API, because Enumerations and Iterators are not considered first-class API entities as in the C++ STL. You're supposed to consume them immediately after creation (ideally implicitly via the "enhanced for loop").

Collections.list()

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 5
    Enumerations cannot be used in the enhanced for loop: http://stackoverflow.com/questions/27240/why-arent-enumerations-iterable – Daniel Rikowski Dec 21 '10 at 14:50