Java ArrayListforEach() Method
Example
Use a lambda expression in theArrayList'sforEach() method to print every item in the list:
import java.util.ArrayList;public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(5); numbers.add(9); numbers.add(8); numbers.add(1); numbers.forEach( (n) -> { System.out.println(n); } ); }}Definition and Usage
TheforEach() method performs an action on every item in a list. The action can be defined by a lambda expression that is compatible with theaccept() method of Java'sConsumer interface.
To learn about lambda expressions, see ourJava Lambda Expression tutorial.
Syntax
public void forEach(Consumeraction)Parameter Values
| Parameter | Description |
|---|---|
| action | Required. AConsumer object or lambda expression which performs an action on an item. |
Related Pages
Java Lambda Expressions Tutorial
❮ ArrayList Methods

