ArrayLists are similar to Java arrays but with dynamic length. The ArrayList grows and shrinks according to the data storage requirement. Each element in ArrayList is stored at a specific and unique index. You can traverse the indexes of ArrayList in a sequence to access the elements. There are different methods to iterate over the elements of the ArrayList such as the forEach() method. The forEach() method is an enhanced version of the for loop but with fewer initializations. 

This article provides examples for the practical demonstration of the Java ArrayList forEach() method. 

How to Use Java ArrayList forEach() Method?

The forEach() method is only used for Arrays and Collection. The forEach() method does not allow the users to access the specific element. The forEach() method increases the code readability and makes it more concise and robust.

Syntax 

The forEach() method has no return type i.e., it does not return any result. Below given is the general syntax of the forEach() method:

ArrayList.forEach( Consumer <E> action )

Here,

  • The ArrayList is an object of the ArrayList class that invokes the forEach() method. 
  • The forEach() method accepts a single parameter “action”. The “action” parameter and the elements of the ArrayList share the same data type.

Example 1: Basic Usage of Java ArrayList forEach() Method

In this code snippet, the ArrayList class is from the “java.util” package which is included by using the following import statement. Inside the main() method, an ArrayList “Employees” is declared. The add() method is invoked using the Employees to add specified strings to the ArrayList. The forEach() method is invoked by the Employees ArrayList. Inside it, the “emp” variable of the same data type is created that iterates over the ArrayList elements. Lastly, the result is then displayed using the “println()” method:

package forEachArrayList;
import java.util.ArrayList; //importing Array List
public class forEachArrayList {
public static void main(String[] args) {
ArrayList <String> Employees = new ArrayList<>();
Employees.add("John");
Employees.add("Jane");
Employees.add("Jeff");
Employees.add("Jennet");

//printing the arrayList
Employees.forEach((emp)->System.out.println(emp));
}
}

Output

The output of the code is given as follows: 

Example 2: Implement Conditions Within the Java ArrayList ForEach() Method  

In this code snippet, the “java.util” package imports the ArrayList class via the import statement:

import java.util.ArrayList; //importing Array List

The ArrayList object “Employees” is created and the add() method is called to add the following elements to it:

ArrayList <String> Employees = new ArrayList<>(); //creating an array list
Employees.add("John");
Employees.add("Jane");
Employees.add("Jeff");
Employees.add("Jennet");

The Employees object invokes the forEach() method. The “emp” object iterates over the elements of the ArrayList. The if-statement determines if the ArrayList current element pointed by the “emp” variable starts with the letter “J”. The statement executes if the startsWith() method returns true and vice versa. The output is then printed:

//printing the ArrayList
Employees.forEach(emp ->
{
  if(emp.startsWith("J"))
  {
      System.out.println("true");
    }
});

Complete Code & Output

Example 3: Implement the Method References With Java ArrayList forEach() Method

Method referencing in the context of the forEach() method further simplifies the syntax for iterating the elements of the ArrayList. Furthermore, it is only invoked when a function is passed as a parameter. 

Inside the main() method,  the “Employees” object is created from the ArrayList class of java.util package. The add() method adds the specified strings to the ArrayList. Lastly, the Employees object invokes the forEach() method that uses the method referencing mechanism. It invokes the println() method of the System.out as shown in the code below:

package forEachArrayList;
import java.util.ArrayList; //importing Array List
public class forEachArrayList {
public static void main(String[] args) {
ArrayList <String> Employees = new ArrayList<>(); //creating an array list
Employees.add("John");
Employees.add("Jane");
Employees.add("Jeff");
Employees.add("Jennet");

//Method referencing
Employees.forEach(System.out::println);
}
}

This sums up the working of the forEach() method with ArrayList in Java.

Conclusion 

The forEach() method functions similarly to the for loop. However, the former is considered more efficient due to its simpler syntax and increased code readability. The forEach() method is invoked by the ArrayList object and the action variable iterates over each element of the array as shown in this guide. You can also specify conditions within the forEach() method. This article is a practical guide with various examples to demonstrate the usage and implementation of the forEach() method with ArrayList in Java.