Several methods for traversing List in java

Source: Internet
Author: User
Tags arrays

1. Use index to traverse the List

2. Use Iterator to traverse the List. (For-each actually uses Iterator)

Note: If only traversal is performed, you can use an index or Iterator to traverse the data. However, if you want to delete elements while traversing, it is best to use Iterator to traverse and delete elements, so as to avoid ConcurrentModificationException exceptions.

The following uses for, for-each, and Iterator to traverse

Instance code:


Import java. util. Arrays;
Import java. util. Iterator;
Import java. util. List;
/**
 *
* Java program to demonstrate different ways to loop, iterate or traverse List in Java.
* There are three example in this program, first examples shows how to loop List
* Using Iterator, Second Example shows Looping over List using advanced Java 5 for loop
* While third and last examples demonstrate use of traditional for loop for traversing
* A List in Java.
 *
* @ Author Javin Paul
*/
Public class ListLoopExample {
  
Public static void main (String args []) {
      
// First example to iterate List in Java using Iterator
List <String> ages = Arrays. asList ("Java", "C ++", "Scala", "Groovy ");
 
 
// Getting Iterator from List in Java
Iterator <String> iterator = ages. iterator ();
System. out. println ("Iterating List in Java using Iterator ");
 
 
// Iterating through all elements of List
While (iterator. hasNext ()){
System. out. printf ("Current element in List is % s % n", iterator. next ());
        }
 
 
// Second example of Iterating over List in Java using foreach loop
System. out. println ("Looping List in Java using foreach loop ");
For (String city: ages ){
System. out. println ("List Element:" + city );
        }
      
// Third example of Looping List using traditional for loop
For (int I = 0; I <ages. size (); I ++ ){
System. out. printf ("programming language # % d in List is: % s % n", I, ages. get (I ));
        }
      
    
    }
}
 
Output:
Iterating List in Java using Iterator
Current element in List is London
Current element in List is Tokyo
Current element in List is NewYork
Current element in List is Mumbai
Looping List in Java using foreach loop
List Element: London
List Element: Tokyo
List Element: NewYork
List Element: Mumbai
City #0 in List is: London
City #1 in List is: Tokyo
City #2 in List is: NewYork
City #3 in List is: Mumbai

Let's look at the example.

Package com. hisense. tiger. list;

Import java. util. ArrayList;
Import java. util. Iterator;
Import java. util. List;

Public class ListTest {
Public static void main (String [] args)
 {
List <String> list = new ArrayList <String> ();
Long t1, t2;
For (int j = 0; j <10000000; j ++)
  {
List. add ("aaaaaa" + j );
  }
System. out. println ("List first visit method :");
T1 = System. currentTimeMillis ();
For (String tmp: list)
  {
// System. out. println (tmp );
  }
T2 = System. currentTimeMillis ();
System. out. println ("Run Time:" + (t2-t1) + "(MS )");
System. out. println ("List second visit method :");
  
T1 = System. currentTimeMillis ();
For (int I = 0; I <list. size (); I ++)
  {
List. get (I );
// System. out. println (list. get (I ));
  }
T2 = System. currentTimeMillis ();
System. out. println ("Run Time:" + (t2-t1) + "(MS )");
  
System. out. println ("List Third visit method :");
Iterator <String> iter = list. iterator ();
  
T1 = System. currentTimeMillis ();
While (iter. hasNext ())
  {
Iter. next ();
// System. out. println (iter. next ());
  }
T2 = System. currentTimeMillis ();
System. out. println ("Run Time:" + (t2-t1) + "(MS )");
    
System. out. println ("Finished !!!!!!!! ");
  
  
 }
}

The test results are as follows:

List first visit method:
Run Time: 170 (MS)
List second visit method:
Run Time: 10 (MS)
List Third visit method:
Run Time: 34 (MS)
Finished !!!!!!!!

The test conclusion is strange. The first method is the new syntax supported by the java language. The code is the most concise, but among the three methods, the performance is indeed the worst, retrieving size has the highest traversal performance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.