*
The moose likes Beginning Java and the fly likes Why does Collections.list(Enumeration<T> e) return ArrayList<T>? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does Collections.list(Enumeration<T> e) return ArrayList<T>?" Watch "Why does Collections.list(Enumeration<T> e) return ArrayList<T>?" New topic
Author

Why does Collections.list(Enumeration<T> e) return ArrayList<T>?

Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75



Isn't it better to return List<T> or Collection<T>? After all, we should code against interfaces, right?


OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

That's a good question. I think that you're right, and it really would be better returning a List. Of course, returning ArrayList gives you access to the additional methods defined in that class, and it's possible that the designers thought that those would be useful in some circumstances. Or possibly it was an oversight on their part. You'd probably have to ask the designers to be sure .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
You cannot simply return a List, unless you instantiate it as an anonymous class.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Leon Omk wrote:Isn't it better to return List<T> or Collection<T>? After all, we should code against interfaces, right?

Right, but what you return has far less to do with programming to interfaces than what you accept. In general, methods should return the most specific type that makes sense, which is one of the reasons why covariant return types were introduced.

It's certainly true that returning an interface may still be the best; however, in the above case, the method is documented as a "converter" between old- and new-style collections, so it makes sense to me that it would return the fastest and most compact type of randomized List.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Campbell Ritchie wrote:You cannot simply return a List, unless you instantiate it as an anonymous class.

Well, true, but you could declare the return value as a List, and then actually return an ArrayList. Seems more consistent to me - you need to know it returns a List, not which specific type.

Edit - though Winston makes a good point. As long as you assign the result to a List, you don't lose anything by having the method declared as returning a specific type.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Campbell Ritchie wrote:You cannot simply return a List, unless you instantiate it as an anonymous class.

But you can declare it as a List, which (I think) is what OP was referring to.

Winston

(Edit) too slow, as usual .
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

Actually Enumeration's purpose is analogous to Iterator. However, it would be more useful to let the method return some sort of collection. The return type probably should have been Iterable, or at most Collection.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Well, with List as the return type you get the advantage that you know that the resulting collection is ordered. But I agree that the return type should have been List<T>, not ArrayList<T>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Stephan van Hulst wrote:The return type probably should have been Iterable, or at most Collection.

I see what you're getting at, but I'm not sure I entirely agree with that. An Enumeration suggests an order - even if that order wasn't guaranteed by the underlying collection. So I think it makes sense to convert to something that can be expected to maintain that order (i.e. a List of some sort).
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

Hmm I guess there's a good point there.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Matthew Brown wrote:I see what you're getting at, but I'm not sure I entirely agree with that. An Enumeration suggests an order - even if that order wasn't guaranteed by the underlying collection. So I think it makes sense to convert to something that can be expected to maintain that order (i.e. a List of some sort).

Totally agree, especially as the method documentation actually says so. Personally, I have no problem with it returning ArrayList, since you can still use it anywhere that takes any of the types mentioned by Stephan (which again, is the method's stated purpose).

It's true that, because of it, someone could pass the result to a method that takes an ArrayList instead of a List; but I think you can take hand-holding a bit too far.

Winston
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Furthermore, by choosing ArrayList as return value Oracle will forever be unable to change the list type to anything other than ArrayList or a sub class of ArrayList.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Sorry, I hadn’t noticed that the method declares that it returns ArrayList. I think Winston Gutkowski’s point is good, that this method is intended for getting rid of legacy code (Enumerations). Agree with Rob about extension problems.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does Collections.list(Enumeration<T> e) return ArrayList<T>?
 
Similar Threads
Why does Collections.list(Enumeration<T> e) return ArrayList<T>?
K&B Java5: Generics question #16 on page 620
why doesnt this compile?
Implementation of Arrays.asList( .. )
converting int array to List