Java ArrayListreplaceAll() Method
Example
Add 1 to every number in a 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(6); numbers.add(1); numbers.replaceAll( n -> n + 1 ); System.out.println(numbers); }}Definition and Usage
ThereplaceAll() method replaces every item in a list with the result of performing an operation on the item. The operation can be defined by a lambda expression that is compatible with Java'sUnaryOperator interface.
To learn about lambda expressions, see ourJava Lambda Expression tutorial.
Syntax
public void replaceAll(UnaryOperatoroperator)Parameter Values
| Parameter | Description |
|---|---|
| operator | Required. AUnaryOperator or lambda expression which operates on each item from the list. |
Related Pages
Java Lambda Expression Tutorial
❮ ArrayList Methods

