ArrayList forEach Method Tutorial

void forEach(Consumer<? super E> action)

You must have a basic understanding of both Generics and Lambda Expressions in order to follow along with this tutorial. I highly recommend watching the following tutorials as a prerequisite for this tutorial:

The forEach() method provides a way to loop through the ArrayList and perform various actions by providing a Lambda expression. In order to explain the parameter list, I will begin by creating a simple ArrayList that will only accept String objects.
ArrayList<String> strList = new ArrayList<>();
strList.addAll(Arrays.asList("Apple", "Banana", "Peach", "Pear"));

Now the documentation syntax for the parameter list (Consumer<? super E> action) may appear somewhat confusing. There are three parts to the documentation for this one single parameter. The first part is the Consumer functional interface. The one and only abstract method in the Consumer functional interface has a method declaration of void accept(T t). Because the Consumer functional interface method accept has a return type of void, our Lambda expression cannot have a return value. Let's begin by declaring a reference variable of type Consumer:
Consumer<String> cLamda;
Notice that I specified the String class inside of diamond syntax as my generic type-variable; that portion corresponds with the <? super E> in the parameter list. Specifying String there will limit the type of objects that can be passed to the Lambda expression to just String in this case. At this point in my tutorial series, attempting to explain the super portion of <? super E> will require some additional concepts, namely Generic wildcards. For now, just match the class or interface type-variable that was specified when the ArrayList was created. Let's assign a Lambda expression to cLambda:
cLambda = x -> System.out.println("Looping through the values: " + x);
In the above expression x can only be of type String because of the original declaration. Now we are ready to execute the forEach() method:
The above statement will display the following lines to the console:
Looping through the values: Apple
Looping through the values: Banana
Looping through the values: Peach
Looping through the values: Pear
As you can see, the forEach() method works just like looping through an ArrayList using an ordinary for statement or an enhanced for statement. Something simliar to this:
for (String x : strList) {
      System.out.println("Looping through the values: " + x);

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md ArrayListForEach
C:\Java>cd ArrayListForEach

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

import java.util.*;
import java.util.function.*;

class ArrayListForEach {
    public static void main(String args[]) {
        ArrayList<String> strList = new ArrayList<>();
        strList.addAll(Arrays.asList("California", "Colorado"));
        Collections.addAll(strList, "Hawaii","New York");

        for(int i=0; i < strList.size(); i++) {
            System.out.println("Regular for statement: "+ strList.get(i));

        for(String s : strList) {
            System.out.println("Enhanced for statement: " + s);

        Consumer<String> cLambda = x -> System.out.println("forEach() method: " + x);

        strList.forEach(x -> System.out.println("forEach() without a Consumer variable: " + x));
        ArrayList<Number> numList = new ArrayList<Number>();
        Collections.addAll(numList, 41, 7.99, 108, 8.34f);
        numList.forEach(x -> System.out.println(x + "   " + x.getClass()));

        Consumer<Number> cNum = x -> System.out.println(x + "   " + x.getClass());

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java ArrayListForEach and press Enter.

C:\Java\ArrayListForEach>java ArrayListForEach
Regular for statement: Alabama
Regular for statement: Alaska
Regular for statement: California
Regular for statement: Colorado
Regular for statement: Hawaii
Regular for statement: New York

Enhanced for statement: Alabama
Enhanced for statement: Alaska
Enhanced for statement: California
Enhanced for statement: Colorado
Enhanced for statement: Hawaii
Enhanced for statement: New York

forEach() method: Alabama
forEach() method: Alaska
forEach() method: California
forEach() method: Colorado
forEach() method: Hawaii
forEach() method: New York

forEach() without a Consumer variable: Alabama
forEach() without a Consumer variable: Alaska
forEach() without a Consumer variable: California
forEach() without a Consumer variable: Colorado
forEach() without a Consumer variable: Hawaii
forEach() without a Consumer variable: New York


41   class java.lang.Integer
7.99   class java.lang.Double
108   class java.lang.Integer
8.34   class java.lang.Float

41   class java.lang.Integer
7.99   class java.lang.Double
108   class java.lang.Integer
8.34   class java.lang.Float

Final thoughts

The forEach() method comes in quite handy for simple loop and display actions. If you need to perform more complex logic, I personally recommend using the more traditional for and enhanced for statements – IMO.