Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Reiterating on Iterators
Posted by Matt Gerrans on October 26, 2001 at 4:31 PM
No, using iterators consistently thoughout your code will do just the opposite; it will increase clarity, maintainability, performance and productivity. Here are the advantages of iterators that I can think of (there are probably others, that don't come to mind at the moment):
- Iterators are more flexible; you don't care what type of collection you are iterating. If you change your implementation from an ArrayList to a HashSet, for example, the code that uses an iterator to go through it will continue to work the same with no change, but the for-loop would break.
- Iterators will usually be faster. Let's say you started with an ArrayList and later changed to a LinkedList (because you found you needed to often add and remove items from throughout the list). The iterator will continue to blaze through at top speed, but the for-loop will be dog slow.
- Iterators are simple. At first glance you might think that the for-loop is simpler than having to learn about this new iterator thing, but after you have developed the habit of using iterators, it will be second nature and you will not have to worry about doing something different for Sets, Lists, Maps, etc. You will also find that Java APIs (and third-party APIs) return iterators (or Enumerations in the older ones - for which it is a good idea to use an Iterator adapter), so it is worth learning.
- They are light-weight. I haven't done any performance testing, but I think you would be hard-pressed to show me an example where the overhead of creating the iterator had any effect on the overall performance relative to using a for-loop (on collections where for-loops will even work).
- Finally and most importantly, saying multi-sylable words like "iterator" in conversation among your collegues will inspire awe in those non-technical folks within earshot, who will think you are an incredible genius. ;-)
Iterators are another good tool that should always be in the top drawer of you software engineering tool chest. - mfg > >The best way to go through all the items in a List (or Vector), >is with an Iterator (see example, below); it is easy to use and >will probably have much better performance than your own for->loop that accesses each item.> iterator's even faster than accesing arraylist items directly? wouldn't the creation of new iterators to go through arraylist items compromise the program clarity/maintainability/performance by creating new objects (iterators) and addition of more codes (lines of code :p)? wouldn't it be simpler to just use a for-loop to check the list's each items? matt.. i'm a bit confused.. mind explaining why do u think using iterator's better? : ) > don't misunderstand, matt.. i'm not being sarcastic here (i have a bad reputation beginning yesterday, so i gotta watch out :p). i do believe u have your reasons for that statement.. mind sharing it with us?
Replies:
- oooo ~ Chin Loong October 26, 2001 at 7:41 PM
(4)
|