Different way to iterate List in Java

Connect with

Oracle Java
There are different way of traversal of ListArrayList, Vector object in java for getting its values. In day-to-day life, we almost use List with different flavor of created object like AraryList, Vector, LinkedList on every alternate day in java programming language, but way of iteration is different , its depend upon the requirement and the required thing in mind on that point of time.

Most of the time while iteration we use JDK 5 for-each loop which is simple and elegant way of iteration if we don’t want to remove while iteration. So , if you asked yourself, what are the different way of iteration or traversal of list in java? how to traversal of its values from vector? how to iterate using while loop in java ? All the above question having one answer with different ways which are as follow. Follow step by step and learn quickly.

5 Different Way to iterate List

  1. Using JDK 5 for-each Loop
  2. Simple For loop
  3. Using Iterator
  4. Using While Loop
  5. Using JDK 8 forEach with stream()

Using JDK 5 for-each

This is simplest of all and elegant of all the iteration, most of the developer use this way very frequently.


// Using JDK 5 for-each Loop "
System.out.println(" \n-----  Using JDK 5 for-each Loop traversal .--------");
for (String value : langList) {
	System.out.println(value);
}

Using Simple for loop


// using simple and plain "for loop"
System.out.println(" \n----- Using simple for loop traversal .--------");
for (int i = 0; i < langList.size(); i++) {
	System.out.println(langList.get(i));
}

Using while loop with Iterator


// Using Iterator with while loop"
System.out.println(" \n-----  Using Iterator with while loop traversal .--------");
Iterator iterator = langList.iterator();
while (iterator.hasNext()) {
	System.out.println(iterator.next());
}


Using while loop with index


// Using while loop with index"
System.out.println(" \n-----  Using While Loop traversal .-------- ");
int i = 0;
while (i < langList.size()) {
	System.out.println(langList.get(i));
	i++;
}

Using JDK 8 forEach with stream()

If you have JDK 8 install in your system then you can run this code. So first make sure of installation of jdk 8. collection stream() util method returns a sequential Stream with this collection as its source.


// Using JDK 8 forEach with stream()
System.out.println(" \n-----  Using JDK 8 forEach with collection stream() ------");
langList.forEach((value) -> {
	System.out.println(value);
});
	}
}

Complete Working Example to iterate over List in different ways


package com.mysoftkey.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * This is java program to traversal / iteration of list, 
 * i.e.  different way of iteration of list.
 * 
 * @author Ranjeet Jha
 *
 */
public class ListTraversalExample {
	
  public static void main(String[] argv) {
		 
	// create a reference of List ,assigned by creating an object of ArrayList.
	List langList = new ArrayList();

	// add few different values to the list
	langList.add("English");
	langList.add("Japanese ");
	langList.add("Hindi");
	langList.add("Russian");
	langList.add("French");

	// using plain "for loop"
	System.out.println(" \n----- Using simple for loop traversal .--------");
	for (int i = 0; i < langList.size(); i++) {
		System.out.println(langList.get(i));
	}

	// Using JDK 5 for-each Loop "
	System.out.println(" \n-----  Using JDK 5 for-each Loop traversal .--------");
	for (String temp : langList) {
		System.out.println(temp);
	}

	// Using Iterator with while loop"
	System.out.println(" \n-----  Using Iterator with while loop traversal .--------");
	Iterator iterator = langList.iterator();
	while (iterator.hasNext()) {
		System.out.println(iterator.next());
	}

	// Using while loop with index"
	System.out.println(" \n-----  Using While Loop traversal .-------- ");
	int i = 0;
	while (i < langList.size()) {
		System.out.println(langList.get(i));
		i++;
	}

	// collection stream() util: Returns a sequential Stream with this collection as its source
	System.out.println(" \n-----  Using JDK 8 forEach with collection stream() ------");
	langList.forEach((temp) -> {
		System.out.println(temp);
	});

  }
}


In the above example you can create ArrayList, Vector, LinkedList, Collections.synchronizedList(List), but iteration remain same, only object of the implementation gets changed.

Program Output


----- Using simple for loop traversal .--------
English
Japanese 
Hindi
Russian
French
 
-----  Using JDK 5 for-each Loop traversal .--------
English
Japanese 
Hindi
Russian
French
 
-----  Using Iterator with while loop traversal .--------
English
Japanese 
Hindi
Russian
French
 
-----  Using While Loop traversal .-------- 
English
Japanese 
Hindi
Russian
French
 
-----  Using JDK 8 forEach with collection stream() ------
English
Japanese 
Hindi
Russian
French


Best way to iterate over List/ArrayList

In the above, using jdk 5 for-each is the best way to iterate . this is simple and elegant way of iteration over any List implementation .


// Using JDK 5 for-each Loop "
System.out.println(" \n-----  Using JDK 5 for-each Loop traversal .--------");
for (String value : langList) {
	System.out.println(value);
}

your comment or suggestions are welcome to improve this post. cheers happy learning 🙂


Connect with

2 thoughts on “Different way to iterate List in Java

  1. Pingback: Smith
  2. Pingback: rakhi

Leave a Reply

Your email address will not be published. Required fields are marked *