3 Ways to Traverse a List

There are primarily 3 ways I can think of to traverse a java.util.List:

  • Using a traditional for loop;
  • Using a simplified for loop, or "foreach" statement in JDK 5 ;
  • Using java.util.Iterator
 public static void traverse(List data) {
System.out.println("Using simplified for loop/foreach:");
for(Object obj : data) {

System.out.println("Using for loop:");
for(int i = 0, n = data.size(); i < n; i++) {

System.out.println("Using Iterator:");
for(Iterator it = data.iterator(); it.hasNext();) {
I always use the for loop and JDK 5 enhanced for loop, and avoid using Iterator to traverse List. If you see other ways, feel free to add them in comment section.

Is there a foreach statement in JDK 1.5? Yes, but the word foreach is not a keyword in java, and I doubt it will ever be. Java foreach statement uses a simplified, or enhanced for loop (see examples above). Therefore, JDK 1.5 doesn't need to introduce new keywords like foreach, forEach, or for each, and foreach/forEach can continue to be used as valid identifier/variable names.


Anonymous said...

Avoiding using the Iterator? You *must* be kidding. Try traversing the LinkedList with 100000 entries using get(i) and see where it gets you with O(N*N) instead of O(N).

Anonymous said...

I'd love to know why the previous commenter would want to use a LinkedList at all. It is almost always better to use ArrayList (do some performance tests, and also consider memory usage...)

howto said...

To be honest, I didn't have LinkedList in mind at the time of writing this blog. I guess it's not common to use a sequential LinkedList. In fact, I haven't used it since I came out of school.

Yes, for LinkedList, using Iterator has better performance, esp for a large list.

The LinkedList javadoc says: "Operations that index into the list will traverse the list from the begining or the end, whichever is closer to the specified index."

Its superclass AbstractSequentialList javadoc says: "This class is the opposite of the AbstractList class in the sense that it implements the "random access" methods (get(int index), set(int index, Object element), set(int index, Object element), add(int index, Object element) and remove(int index)) on top of the list's list iterator, instead of the other way around."

Anonymous said...

Stephen, let's put it this way. We all know to "program to the interface". So, if you write an API that gets a List, then you should expect dealing efficiently with any List implementation, including LinkedList. So, it's not the question of when do *you* use the LinkedList. As the API writer, you can not force the implementation on the users. And believe me, there are good examples of using LinkedList. ArrayList just looks like the golden hammer to those that came from C's arrays.

javin paul said...

Nice article. There is another similar question usually asked in interview is four way of getting elements from hashMap which I have included here <a href="http://javarevisited.blogspot.com/2011/04/top-20-core-java-interview-questions.html>Top 20 Core Java Interview question</a>


Unknown said...

You can find out the cycle or loop in a single linked list by using two pointer approach.
Below link can be useful to find out the algorithm to find cycle or loop in linked list

Find out loop or cycle in linked list in java


Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Unknown said...

Thank you for another fantastic posting. Where else could anyone get that kind of information in such a perfect way of writing? I have a speech next week, and I was looking for more info.
kizidaily , juegosfrivas , friv2gamers

Beatrise said...

Thanks for sharing your information with everyone and keep posting

sherin said...

more informative post
how to enter steam wallet code

Anonymous said...

Read the article if you still don't know whether you need to file w2 form or not.

Sophie Grace said...

Thanks for the best blog was very useful for me. Keep sharing such ideas in the future as well. Thanks for giving me the useful information. webstagram

Shivam Kumar said...

Thanku for sharing such good post.keep this work up. visit LinkedList example

Shivam Kumar said...

Thanku for sharing such good post.keep this work up. visit LinkedList example

Jack sparrow said...

That is nice article from you , this is informative stuff . Hope more articles from you . I also want to share some information about online devops training and devops online training