Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a one-liner to create a Collection from an Enumeration object?

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?

like image 707
Daniel Rikowski Avatar asked Dec 21 '10 14:12

Daniel Rikowski


People also ask

Can we use enumeration on ArrayList?

In order to get enumeration over ArrayList with Java Collections, we use the java. util. Collections. enumeration() method.

How do you enumerate a list in Java?

Enumeration[] array = Enumeration. values(); List<Enumeration> list = Arrays. asList(array);


2 Answers

In fact, there is Collections.list(enumeration)

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

like image 186
Bozho Avatar answered Nov 15 '22 11:11

Bozho


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()

like image 34
Michael Borgwardt Avatar answered Nov 15 '22 10:11

Michael Borgwardt