Java LinkedListforEach() Method
Example
Use a lambda expression in theLinkedList'sforEach() method to print every item in the list:
import java.util.LinkedList;public class Main { public static void main(String[] args) { LinkedList<Integer> numbers = new LinkedList<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
❮ LinkedList Methods

