Data structures are an important aspect of all the programming languages. They help to store the data in a proper manner and retrieve it when needed. Data structures in Java include lists, queues, stacks, arrays, ArrayList, etc. Each has its own methods and functions in Java. While working with these data structures sometimes the need to iterate through these data structures arises. For this purpose, different loops are used in programming. However, a built-in forEach() method can be used while working with the ArrayLists to achieve the same functionality.
This article will advance in the following manner to understand the working of the for-each loop on the ArrayList in Java.
- What is An ArrayList in Java?
- What is the forEach() Method of ArrayList in Java?
- Implementation of forEach() on the Strings
- Implementation of the forEach() on the Integers
- Lambda Expression With the forEach() method
- NullPointerException
- Conclusion
What is An ArrayList in Java?
The ArrayList in Java belongs to the “java.util” package of the collection framework. Because of its dynamic nature, there is no need to declare the size of the array. The elements in the ArrayList can be inserted or deleted anytime according to the requirement of the code. The ArrayList implements different methods like add(), remove(), etc. to perform different functionalities on the ArrayList in Java.
What is the forEach() Method of the ArrayList in Java?
The forEach() method of the ArrayList in Java performs certain actions for every element present in the ArrayList. All the elements of the ArrayList are processed and each element undergoes the specified action to print the outcome. Let us scan through the syntax of the forEach() method in Java.
public void forEach(Consumer<? super E> action)
In the above syntax:
- The “action” is to be performed on every element.
- This method does not return anything.
Implementation of forEach() on the Strings
The code below shows the working of the forEach() method of the ArrayList in Java on the strings.
//Import the package
import java.util.*;
//declare the class
public class Example1 {
public static void main(String[] args)
{
// string values
ArrayList<String>employee = new ArrayList<String>();
employee.add("John");
employee.add("Smith");
employee.add("Adam");
// print the results
System.out.println("Before forEach() Method:" + employee);
System.out.println("AFTER forEach() Method");
//forEach() method to print the employee name
employee.forEach((e) -> print(e));
}
public static void print(String e)
{
System.out.println("The Employee List is: " + e);
}
}
In the above code block:
- The required package of Java is imported and the class is declared as “Example1”.
- An object “employee” is declared for the ArrayList which further uses the add() method to insert the strings to the ArrayList.
- The output before the forEach() method is printed.
- The forEach() method moves to every element of the ArrayList.
- In the main() method of Java, the println() method prints the output.
Output
The output below shows the ArrayList before the implementation of the forEach() method and the Arraylist after the forEach() method in Java.

Implementation of forEach() on the Integers
The code below states the utilization of the forEach() method on the integers declared in the ArrayList in Java.
//Import the package
import java.util.*;
//declare the class
public class Example1 {
public static void main(String[] args)
{
// string values
ArrayList<Integer>values = new ArrayList<Integer>();
values.add(25);
values.add(33);
values.add(75);
values.add(100);
// print the results
System.out.println("Before forEach() Method:" + values);
System.out.println("AFTER forEach() Method");
//forEach() method to print the employee name
values.forEach((e) -> print(e));
}
public static void print(Integer e)
{
System.out.println("The Integer List is: " + e);
}
}
In the above Java code:
- The Integers are declared in the ArrayList.
- The forEach() method in Java proceeds to every integer in the ArrayList and the result is printed using the println() method.
Output
The output shows the original ArrayList and the ArrayList after the forEach() method is implemented.

Lambda Expression the forEach() Method
The lambda expressions in Java are referred to as the “shortcode block” which takes the parameters and returns the values against the specified parameters. The forEach() method of the ArrayList can be implemented on the Lambda expressions in Java as depicted in the code below.
//Import the package
import java.util.*;
//declare the class
public class Example1 {
public static void main(String[] args)
{
// string values
ArrayList<Integer>values = new ArrayList<Integer>();
values.add(5);
values.add(3);
values.add(7);
values.add(10);
// print the results
System.out.println("Before forEach() Method:" + values);
System.out.println("AFTER forEach() Method");
//forEach() method
values.forEach((e) -> {
e = e + e;
System.out.println("The Integer is: " + e);
});
}
}
In the above code block:
- The forEach() method is used in the lambda expression to iterate through the ArrayList and returns the double of each value.
- The integers are printed to the output using the println() method.
Output
The output below shows that the original integer list and the double of the list are printed using the declared lambda expression.

NullPointerException
The code below throws the NullPointerException while working with the forEach() method in Java.
//Import the package
import java.util.*;
//declare the class
public class Example1 { //The Main class of Java
public static void main(String[] args)
{
// string values
ArrayList<Integer>values = new ArrayList<Integer>();
values.add(25);
values.add(33);
values.add(75);
values.add(100);
// print the results
System.out.println("Before forEach() Method:" + values);
System.out.println("AFTER forEach() Method");
//forEach() method to print the employee name
values.forEach((null));
}
public static void print(Integer e)
{
System.out.println("The Integer List is: " + e);
}
}
In the above code block:
- The integers are added to the ArrayList.
- In the next step, the output before the implementation of the forEach() method is printed.
- The forEach() method in the next step accepts a null value and the result is printed using the println() method.
Output
The output below shows that the ArrayList before the method is printed whereas for the second part of the code block a “NullPointerException” is raised when “null” is passed to the forEach() method.

That’s all about the utilization of the forEach() method in different scenarios.
Conclusion
The forEach() method processes every element present in the ArrayList in Java. It has multiple use cases, however, it is commonly implemented when we have to perform some specific functionality on each element of the ArrayList. In this article, we have implemented the forEach() method of the ArrayList on strings, numbers, and lambda expressions.