Adam Lipscombe wrote:
Are there any advantages in speed in passing a List or an Iterator as
a method parameter, or as a method return value?
Nothing known to me, and if there's any difference at all
I'd expect it to be so small that it would be difficult to
measure with confidence.
Any runtime reasons why one should be preferred over the other?
Yes, of course: There are things that can be done with a
List that cannot be done with an Iterator. If you need to sort
or shuffle the List, or peruse its contents in "random" order,
or replace an existing element with a different one, or insert
an element, or extract a sub-List, or ... All you can do with
an Iterator is visit the List's elements in whatever order the
Iterator dictates, and (perhaps) remove the most recently visited
element. Heck, given only an Iterator you can't even find the
List's size! (Lest anyone be thinking "Iterate and count," answer
me this: Given only an Iterator, how many times has its next()
method already been called?)
Decide what you're trying to do, and *then* decide how to
do it. Horse the before cart the put don't.
Point taken. thanks.