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.
The moose likes Java in General and the fly likes Which is better? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Which is better?" Watch "Which is better?" New topic
Author

Which is better?

Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
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.


"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
C. Use an enhanced for loop (from JDK 5)

Or failing that,

D. Use a for loop with an Iterator.


"I'm not back." - Bill Harding, Twister
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
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

Joined: Sep 16, 2005
Posts: 1780
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 ]

There is no emoticon for what I am feeling!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is better?
 
Similar Threads
Array of ArrayLists Possible?
Which is Best Way to Get a List for JSP
Help with An Array Of Objects
store list of names in a variable
Line break