6/06/2006

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(obj);
}

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

System.out.println("Using Iterator:");
for(Iterator it = data.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
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.

PS:
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.

5 comments:

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).

Stephen Colebourne 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>

Thanks
Javin