Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is better?

 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ranchers,

Which is better in parsing a List?

a. Use a Collection and Iterator to iterate each record.

or

b. Use a for loop and use get method of the list.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C. Use an enhanced for loop (from JDK 5)

Or failing that,

D. Use a for loop with an Iterator.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesper de Jong and Jim Yingst,

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?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your a) is his d):
In 1.5, I would use the enhanced for loop and generics:

[ December 09, 2005: Message edited by: Jeff Albrechtsen ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Jeff]: I think your a) is his d):

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic