Because I may re-utilize the instance returned by the method "next ()" without making a new one. For example I can re-fill an array with new values without making a new instance on every iteration.
SCJP, SCWCD.
|Asking Good Questions|
Igor Nadal wrote:I'm sure that this obsession to re-use objects comes from having read about the use of StringBuilder instead of String into for/while loops.
Why do you think that is? I think that it doesn't save memory. The data needs to be stored somewhere in the memory and whether you're using an Iterator or a reference to it won't make that much of a difference (maybe there is even a small overhead for the Iterator instance).Igor Nadal wrote:[...]
Because it saves memory, since you don't need to return a bunch of data.
[...]
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet wrote:
Why do you think that is? I think that it doesn't save memory. The data needs to be stored somewhere in the memory and whether you're using an Iterator or a reference to it won't make that much of a difference (maybe there is even a small overhead for the Iterator instance).Igor Nadal wrote:[...]
Because it saves memory, since you don't need to return a bunch of data.
[...]
Mike Simmons wrote:I was a bit surprised to see that Guava's IO code does not appear to use Iterators or Iterable in many places I would have expected them to. .... Seems like it's destined to remain a bit cumbersome to use, even if it is pretty efficient.
Martin Vajsar wrote:As for returning Iterable: it's nice idea, until now I was usually returning a Collection. Using it for "streamed" data has one minor disadvantage though: I'd expect that I can iterate over an Iterable instance more than once. Again, this limitation would have to be thoroughly documented, especially if the instance you'd return might be eventually used a few layers away from your method.
Igor Nadal wrote:Mike Simmons: I faced that problem which you comment several weeks ago indeed. I made my own interface for these cases... But re-doing my personal libraries I forgot why I had needed such interface and decided to replace the returned class by one which implemented Iterator, in order to make it more portable. So now, I'm hitting my head on the desk, ha ha ha. So I'm turning back to the old approach.
Igor Nadal wrote:but it's possible to happen if I miss something, absolutely.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |