5 ways to iterate List in Java

Connect with

5 ways to iterate List in Java5 ways to iterate List in Java, traversal of Java List or ArrayList, Vector, LinkedList object to get its values. using for-each, for loop, iterator, while loop, jdk 8 forEach.stream().

1. Different Ways to iterate List in Java

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

2. overview of ways to iterate List in Java

In day-to-day life, we almost use List with different implementation like AraryList, Vector, LinkedList on every day in Java programming language, but ways of iteration is different time to time, it depends upon the requirement and the required thing in mind on that point in time.

Most of the time while traversing, we use JDK 5 for-each loop which is a simple and elegant way of iteration if we don’t want to remove while iteration. So, if you asked yourself, what are the different ways of iteration or traversal of the list in java? how to the traversal of its values from vector? how to iterate using a 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.

3. 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);
}

4. Using Simple for loop to iterate Java List

Simple for loop is one of the ways of iterting Java List.

// 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));
}

5. Using while loop with Iterator

This is one of the ways of iterting Java List.

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


6. Using while loop with index

Using while loop you can also iterate java list and this is one of the ways to iterate List in Java.

// 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++;
}

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

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

9. Program Output for traversing in Java

Following are the output of all the 5 ways of iteration of Java List.

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


10. Best way to iterate over List/ArrayList

In the above, using JDK 5 for-each is the best way to iterate. This is a simple and elegant way of iteration over any List implementation if you are not removing elements while iterate.

// 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);
}

visit Java tutorial for more details.

Visit Oracle Java for more details.
your comment or suggestions are welcome to improve this post of different ways to iterate List in Java. cheers happy learning 🙂


Connect with

2 thoughts on “5 ways to iterate List in Java”

  1. Pingback: Smith

  2. Pingback: rakhi

Leave a Comment

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