Java - When to use Iterators?

Posted by Walter White on Stack Overflow See other posts from Stack Overflow or by Walter White
Published on 2010-04-01T00:33:03Z Indexed on 2010/04/01 0:43 UTC
Read the original article Hit count: 530

Hi all,

I am trying to better understand when I should and should not use Iterators. To me, whenever I have a potentially large amount of data to iterate through, I write an Iterator for it. If it also lends itself to the Iterator interface, then it seems like a win.

I was reading a little bit that there is a lot of overhead with using an Iterator.

A good example of where I used an Iterator was to iterate through a bunch of SQL scripts to execute one query at a time, reading it in, then executing it.

Is there another performance trade off I should be aware of? Before I used iterators, I would read the entire String of SQL commands to execute into an ArrayList, and the iterate through that. If the import is rather large (like for geolocation data, then the server tends to get bogged down).

Walter

© Stack Overflow or respective owner

Related posts about java

Related posts about Performance