views:

46

answers:

3

Hi,

Why is java.util.Collections.list only for Enumeration but not for Iterator (or Iterable)? Or why is there not an overload of this function for Iterator (or Iterable)? Is there any other method which does that? Is there any reason why that is?

+1  A: 

From the JavaDoc:

Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that require collections.

If you have a Collection and want to make new List based on that collection, you can use constructors or the addAll() method. This method is somthing like an adapter provided by the collections framework.

For custom iterables you can use the enhanced for loop to copy the elements returnd by an iterator to an existing list.

Andreas_D
Yes but why is there no function for `Iterator` or `Iterable`?
Albert
I know what I could do instead to work around this. But my question is not about that. It is about: Why is there no function for this already if there is one for `Enumeration`?
Albert
@Albert - I'd ask the other way round: why did they implement that `list` method anyway? Usually there's no need for such a specialized method in such a prominent utility class. Even with java 1.4, it takes just 2-4 lines of code.
Andreas_D
A: 

it's purpose is to provide interoperability between legacy APIs that return enumerations and new APIs that require collections

Jeroen Rosenberg
A: 

Noone really answered it yet, so here I go:

When it what introduced, there probably was no Iterable yet. Now, it is obsolete (constructors of most Collection implementations provide the same thing) and just stays there for backward compatibility.

Albert