This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
a. Use a Collection and Iterator to iterate each record. b. Use a for loop and use get method of the list.
It depends on what implementation of interface List you are using.
If you are using a LinkedList, option a will definitely be better than option b, because in a LinkedList looking up an entry by index means that you have to traverse through the list, counting the entries until you reach the one you're looking for. The API documentation of LinkedList says:
All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the begining or the end, whichever is closer to the specified index.
For an ArrayList there's probably not a big difference between a and b.
I'm currently using jdk 1.4. I'm also using here an ArrayList that would basically hold resultsets retrieved from the database and I want this to parse the whole resultset using the ArrayList. And I am thinking of how am I gonna parse the list whether use an Iterator or simply loop for the list.
Jim, I didn't get the loop then use Iterator. Can you show me an example?
Originally posted by Richard Rex: I'm currently using jdk 1.4. I'm also using here an ArrayList that would basically hold resultsets retrieved from the database and I want this to parse the whole resultset using the ArrayList. And I am thinking of how am I gonna parse the list whether use an Iterator or simply loop for the list.
Well, without actually looking at the source code, I would expect that the iterator of the ArrayList class would just use the get method under the covers. Therefore, I would expect that, in terms of outright performance, using the get method directly would be nominally faster.
The real power of using an Iterator is that you do not need to know the underlying structure of your collection in order to get the data out of it. It sounds like you're controlling the creation of list, as well as getting the data out of it, so it's probably not a big deal. In addition, as long as you program to the interface List, rather than the concrete implementation, ArrayList, you ward off some trouble. For example, List defines the get method so, if you decide you wanted to change from an ArrayList to a LinkedList, you'd still be fine invoking get on your collection. So, using a List interface and simply looping through the objects invoking the get method would be a good way to go about this.
The question you need to ask is whether or not your collection might ever change from a plain list into something more complex, like a map or a composite. Most likely, it won't. However, if it does, using an Iterator would make your life easier, as your retrieval code won't have to change. I suspect that this isn't an issue that you necessarily need to concern yourself with, but it may be something to look at.
Maybe. You showed code matching what I was thinking of for d, dunno if it's his a. Since I for loop was mentioned prominently for b but not a, I thought Richard was impying a did not use a for loop. I've seen the question many times in the past - which is faster, a for loop or an Iterator? Somehow many people think these are exclusive, which is incorrect. I don't know if that's what Richard thought or not.
[Corey]: For example, List defines the get method so, if you decide you wanted to change from an ArrayList to a LinkedList, you'd still be fine invoking get on your collection.
Your code would work, but performance could very well become horrible (especially if the list is big). For an ArrayList, get() has good performance, but for a LinkedList it's very poor. (It's the opposite for some other operations.) If you use an Iterator, the performance is good, very close for both ArrayList and LinkedList. (Close enough the difference probably doesn't matter.) So, the possibility of changing the ArrayList to a LinkedList is one good reason not to use get(). And as you note, the possibility of changing to any other Collection type is another good reason.