I recommend using an Iterator in a for loop. These two options are not exclusive.
I'm guessing you mean, what is the advantage of using an Iterator rather than a get(index) method? Well, sometimes the get(index) is your only option, and sometimes it's slighhtly faster than an Iterator. However other times, it can be much, much slower than an Iterator. A LinkedList is a classic example. This class has a get(index) method, but it is horribly, horribly slow. Well, it's not that bad if the list is short, or if you're looking for an item that is close to the beginning or end. But if frequently will be much slower than an Iterator will be. And in many cases, you don't know for sure whether you want a LinkedList or an ArrayList or some other List implementation. If you get(index), you will get the fasted possible response from an ArrayList, but you will get a very poor response from a LinkedList. If you use an Iterator, you will get something fairly close to the fastest possible response from an ArrayList, and you will get the fastest possible response from a LinkedList. So in general, an Iterator is a more reliable choice. It's not always the fastest possble choice, but it's always close. And it protects you from the extremely slow behavior you would get if you mistakenly used a get(index) on a LinkedList.
Also, since JDK 5, the enhanced for loop will automatically use a hidden Iterator any time you try to loop over any Collection (or more generallly, any Iterable). So in many cases, using an Iterator is drectly supported by the language so that it's the most convenient thing for you to do - as well as being fastest, or close to fastest.
"I'm not back." - Bill Harding, Twister
Joined: Feb 02, 2003
Thank you very much Jim Yingst . Could you please tell me whether there is any Thread related issue for both the cases?
Joined: Jan 30, 2000
If more than one thread can access a Collection (or Map or array or other group-of-things), you need to synchronize. (Or use something like java.util.concurrent.locks classes from JDK 5+). It's a bad idea if one thread can change a Collection while another is trying to iterate through it. This is true whether you use get(index) or an Iterator. The main difference is that most (not all) commonly-encountered implementations of Iterator will throw a ConcurrentModificationException if you fail to synchronize correctly. Whereas if you use get(index) you probably won't get an exception; you'll just get bad data. Which in my opinion, is usually worse, because it's harder to find the real problem. A CME is a nice bright neon sign saying "more than one thread is accessing this collection simultaneously, and that is WRONG!" Hiding errors is Evil; making them easy to find is Good. It is never guaranteed that Iterator will throw CME if a problem occurs, but chances are good. You shouldn't rely on this behavior, but it's nice to have it as an extra layer of protection against mistakes. So in my opinion, this is another reason to prefer Iterator in most cases.