Java 8 – Iterating List using forEach() method

In previous article, we have discussed various ways to iterate through List i.e.;

Various ways to iterate through List:

  1. regular for-loop
  2. Enhanced for-loop introduced in Java 1.5 version
  3. Iterating using Iterator of Collection interface
  4. Iterating using ListIterator of List interface
  5. Read different ways to iterate List

1. Iterating List using enhanced for-loop introduced in Java 1.5 version

  • With enhanced for-loop we can iterate through List as demonstrated below,

IteratingListUsingEnhancedForLoop.java

package in.bench.resources.java.collection.list;

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

public class IteratingListUsingEnhancedForLoop {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		List<String> founders = new ArrayList<String>();

		// adding elements to ArrayList object
		founders.add("Sundar Pichai");
		founders.add("Satya Nadella");
		founders.add("Shiv Nadar");
		founders.add("Shantanu Narayen");
		founders.add("Sundar Pichai"); // duplicate object
		founders.add("Francisco D’Souza");
		founders.add(null); // null is allowed

		// Iterating using enhanced for-loop
		System.out.println("Iterating using enhanced for-loop\n");
		for(String founder : founders) {
			System.out.println(founder);
		}
	}
}

Output:

Iterating using enhanced for-loop

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null

2. Iterating ArrayList using enhanced for-each loop introduced in Java 1.8 version

  • In Java 8, we can iterate List using Iterable.forEach() loop

IteratingListUsingForEachLoopInJava8.java

package in.bench.resources.java.collection.list;

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

public class IteratingListUsingForEachLoopInJava8 {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		List<String> founders = new ArrayList<String>();

		// adding elements to ArrayList object
		founders.add("Sundar Pichai");
		founders.add("Satya Nadella");
		founders.add("Shiv Nadar");
		founders.add("Shantanu Narayen");
		founders.add("Sundar Pichai"); // duplicate object
		founders.add("Francisco D’Souza");
		founders.add(null); // null is allowed

		System.out.println("Iterating using forEach() in Java 8\n");

		//  Iterating using forEach() in Java 8
		founders.forEach(founder -> System.out.println(founder));
	}
}

Output:

Iterating using forEach() in Java 8

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza
null

Notice, line no. 25 for forEach statement –> which is doing magic here i.e.; simplifying earlier for-loop

From above example, List

  • allows duplicate elements
  • null object is allowed
  • while iterating insertion-order is maintained

Related Articles:

To conclude, now there are 5 ways to iterate List

References:

Happy Coding !!
Happy Learning !!

Java - How to get size or length of an ArrayList
Java - Iterate through ArrayList in 5 ways