Do you wonder which of the following methods are the fastest way to loop through a list or collection before?

  1. While Loop
  2. For Loop
  3. Iterator Loop

Performance Test – While , For and Iterator

Here i create a simple program to loop through a List with 1,5,10 and 15 millions records in While loop, For loop and Iterator loop.

Here’s the source code to run the simulating the testing date.

package com.mkyong.core;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
public class ArrayToList {
	public static void main(String[] argv) {
		String sArray[] = createArray();
		// convert array to list
		List lList = Arrays.asList(sArray);
		System.out.println("\n--------- Iterator Loop -------\n");
		long lIteratorStartTime = new Date().getTime();
		System.out.println("Start: " + lIteratorStartTime);
		// iterator loop
		Iterator<String> iterator = lList.iterator();
		while (iterator.hasNext()) {
			String stemp =;
		long lIteratorEndTime = new Date().getTime();
		System.out.println("End: " + lIteratorEndTime);
		long lIteratorDifference = lIteratorEndTime - lIteratorStartTime;
		System.out.println("Iterator - Elapsed time in milliseconds: "
				+ lIteratorDifference);
		System.out.println("\n--------- For Loop --------\n");
		long lForStartTime = new Date().getTime();
		System.out.println("Start: " + lForStartTime);
		// for loop
		for (int i = 0; i < lList.size(); i++) {
			String stemp = (String) lList.get(i);
		long lForEndTime = new Date().getTime();
		System.out.println("End: " + lForEndTime);
		long lForDifference = lForEndTime - lForStartTime;
		System.out.println("For - Elapsed time in milliseconds: "
				+ lForDifference);
		System.out.println("\n--------- While Loop -------\n");
		long lWhileStartTime = new Date().getTime();
		System.out.println("Start: " + lWhileStartTime);
		// while loop
		int j = 0;
		while (j < lList.size()) {
			String stemp = (String) lList.get(j);
		long lWhileEndTime = new Date().getTime();
		System.out.println("End: " + lWhileEndTime);
		long lWhileDifference = lWhileEndTime - lWhileStartTime;
		System.out.println("While - Elapsed time in milliseconds: "
				+ lWhileDifference);
	static String[] createArray() {
		String sArray[] = new String[15000000];
		for (int i = 0; i < 15000000; i++)
			sArray[i] = "Array " + i;
		return sArray;


D:\test>java -Xms1024m -Xmx1024m ArrayToList
--------- Iterator Loop -------
Start: 1232435614372
End: 1232435614763
Iterator - Elapsed time in milliseconds: 390
--------- For Loop --------
Start: 1232435614763
End: 1232435614997
For - Elapsed time in milliseconds: 234
--------- While Loop -------
Start: 1232435614997
End: 1232435615232
While - Elapsed time in milliseconds: 234

Performance Test Result (in milliseconds)


Well… the result show that the Iterator mechanism is the slowest method to loop through a list. There’s not much performance different between For and While loop.

Iterator provides a very handy way to loop through a list or collection, but it is slower than For and While loop. Please be remind that the different is just in milliseconds (not even 1 second for 15 millions of data looping). So, just choose any looping mechanism you want, there’s not much different.

15 millions of data and java -Xms1024m -Xmx1024m is the limit of my computer , may be someone can help me test this in 20 millions of data? Thanks ~
Tags :
Founder of, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts