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 .
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.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
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.
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).
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.
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.
Joined: Oct 13, 2005
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.