.forEach()
The .forEach()
method performs a specified action on each element in an ArrayList
. This method traverses each element in the ArrayList
until all the elements have been processed or an exception is raised from the action. It only iterates through the elements without modifying the ArrayList
and does not return any value.
Syntax
arrayListInstance.forEach(Consumer<E> action);
arrayListInstance
: The name of theArrayList
to be iterated.Consumer
: A functional interface representing an operation to be performed. It can be used as the assignment target for a lambda expression or method reference.action
: The operation that accepts an element of typeE
in theArrayList
as its only argument and does not return any value.
Example
The following example uses the .forEach()
method with a lambda expression to print out the elements in an ArrayList
called students
:
// Import the ArrayList class from the java.util packageimport java.util.ArrayList;public class Main {public static void main(String[] args) {// Create an ArrayList of strings called 'students'ArrayList<String> students = new ArrayList<>();students.add("Anna");students.add("Bailey");students.add("Chris");students.add("Donna");students.forEach((s) -> System.out.println(s));}}
The above example produces the following output:
AnnaBaileyChrisDonna
Apart from using a lambda expression, each student in the ArrayList
can also be printed out using a method reference:
import java.util.ArrayList;public class Main {public static void main(String[] args) {ArrayList<String> students = new ArrayList<>();students.add("Anna");students.add("Bailey");students.add("Chris");students.add("Donna");students.forEach(System.out::println);}}
The output matches with the previous one:
AnnaBaileyChrisDonna
The operation passed into .forEach()
is meant to be performed on each element of the ArrayList
, not the ArrayList
itself. Therefore, if an operation to be performed on the ArrayList
is passed into .forEach()
, an error occurs.
In the following example, the .get()
operation passed into .forEach()
is to be performed on students
to retrieve a student name from the ArrayList
:
import java.util.ArrayList;public class Main {public static void main(String[] args) {ArrayList<String> students = new ArrayList<>();students.add("Anna");students.add("Bailey");students.add("Chris");students.add("Donna");students.forEach(System.out.println(students.get(0)));}}
The above code will throw an error:
Main.java:12: error: 'void' type not allowed herestudents.forEach(System.out.println(students.get(0)));^1 error
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly16 hours