Make sure you have
- Eclipse Kepler
- M2Eclipse plugin
- Java 1.7
- Tomcat 7
tryPlease note: If the JVM exits while the try or catch code is being executed, then the finally block will not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block will not execute even though the application as a whole continues.
{
if (userInput)
{
while (true) ;
}
else
{
System.exit(1);
}
}
finally
{
// what ever you want to clean up
}
For the past 13 years, Java has changed our world . . . and our expectations..
Today, with technology such a part of our daily lives, we take it for granted that we can be connected and access applications and content anywhere, anytime. Because of Java, we expect digital devices to be smarter, more functional, and way more entertaining.
In the early 90s, extending the power of network computing to the activities of everyday life was a radical vision. In 1991, a small group of Sun engineers called the "Green Team" believed that the next wave in computing was the union of digital consumer devices and computers. Led by James Gosling, the team worked around the clock and created the programming language that would revolutionize our world – Java.
The Green Team demonstrated their new language with an interactive, handheld home-entertainment controller that was originally targeted at the digital cable television industry. Unfortunately, the concept was much too advanced for the them at the time. But it was just right for the Internet, which was just starting to take off. In 1995, the team announced that the Netscape Navigator Internet browser would incorporate Java technology.
Today, Java not only permeates the Internet, but also is the invisible force behind many of the applications and devises that power our day-to-day lives. From mobile phones to handheld devises, games and navigation systems to e-business solutions, Java is everywhere!
Do you wonder which of the following methods are the faster way to loop through a list or collection before?
1) While Loop
2) For Loop
3) Iterator Loop
Here i create a simple program to loop through a List with 1,5,10 and 15 millions of data in While loop, For loop and Iterator loop. It’s will calculate and display the elapsed time in output.
Here is the performance test source code
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
while ( iterator.hasNext() ){
String stemp = iterator.next();
}
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-------END-------");
System.out.println("\n--------- For Loop --------\n");
long lForStartTime = new Date().getTime();
System.out.println("Start: " + lForStartTime);
//for loop
for (int i=0; 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-------END-------");
System.out.println("\n--------- While Loop -------\n");
long lWhileStartTime = new Date().getTime();
System.out.println("Start: " + lWhileStartTime);
//while loop
int j=0;
while (j<>
{
String stemp = (String)lList.get(j);
j++;
}
long lWhileEndTime = new Date().getTime();
System.out.println("End: " + lWhileEndTime);
long lWhileDifference = lWhileEndTime - lWhileStartTime;
System.out.println("While - Elapsed time in milliseconds: " + lWhileDifference);
System.out.println("\n-------END-------");
}
static String [] createArray(){
String sArray[] = new String [15000000];
for(int i=0; i<15000000;>
sArray[i] = "Array " + i;
return sArray;
}
}
Output
D:\test>java -Xms1024m -Xmx1024m ArrayToList
--------- Iterator Loop -------
Start: 1232435614372
End: 1232435614763
Iterator - Elapsed time in milliseconds: 390
-------END-------
--------- For Loop --------
Start: 1232435614763
End: 1232435614997
For - Elapsed time in milliseconds: 234
-------END-------
--------- While Loop -------
Start: 1232435614997
End: 1232435615232
While - Elapsed time in milliseconds: 234
-------END-------
Performance Test Result (in milliseconds)
Well… the result show 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 performance different.