Introduction to ArrayList.replaceAll()
The replaceAll()
method in Java's ArrayList provides a concise way to modify each element within the list based on a provided function (UnaryOperator). It avoids the need for manual iteration and assignment, making your code cleaner and more efficient when you want to transform all elements of an ArrayList.
Syntax
public void replaceAll(UnaryOperator<E> operator)
Parameters
Parameter | Description |
---|---|
operator |
A UnaryOperator<E> that defines the operation to be performed on each element of the list. It takes an element of type E as input and returns a modified or transformed value, also of type E . |
Return Value
This method doesn't return any value; it operates directly on the ArrayList in place (void).
Examples
Example 1: Squaring all numbers in an ArrayList
Let’s say you have an ArrayList of integers, and you want to square each element. The replaceAll()
method makes this easy.
import java.util.ArrayList;
import java.util.List;
import java.util.function.UnaryOperator;
public class ReplaceAllExample1 {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(
List.of(1, 2, 3, 4, 5)
);
UnaryOperator<Integer> square = x -> x * x;
numbers.replaceAll(square);
System.out.println(numbers); // Output: [1, 4, 9, 16, 25]
}
}
[1, 4, 9, 16, 25]
In this example, we create an ArrayList of integers. We define a UnaryOperator
named square
that takes an integer as input and returns its square. Then, we call replaceAll()
with the square
operator. This applies the squaring function to each element in the list, effectively modifying the original ArrayList.
Example 2: Converting Strings to Uppercase
This example demonstrates how to use replaceAll()
to transform strings within an ArrayList.
import java.util.ArrayList;
import java.util.List;
import java.util.function.UnaryOperator;
public class ReplaceAllExample2 {
public static void main(String[] args) {
List<String> names = new ArrayList<>(List.of("alice", "bob", "charlie"));
UnaryOperator<String> toUpperCase = String::toUpperCase;
names.replaceAll(toUpperCase);
System.out.println(names); // Output: [ALICE, BOB, CHARLIE]
}
}
[ALICE, BOB, CHARLIE]
Here, we have an ArrayList of strings. We use a method reference String::toUpperCase
as the UnaryOperator
to convert each string to uppercase. The replaceAll()
method then applies this conversion to every element in the list.
Example 3: Adding a Constant Value to Each Element
This example showcases adding a constant value to each number in an ArrayList.
import java.util.ArrayList;
import java.util.List;
import java.util.function.UnaryOperator;
public class ReplaceAllExample3 {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(List.of(10, 20, 30));
int addValue = 5;
UnaryOperator<Integer> addConstant = x -> x + addValue;
numbers.replaceAll(addConstant);
System.out.println(numbers); // Output: [15, 25, 35]
}
}
[15, 25, 35]
In this case we create an ArrayList of integers and add a constant value (5) to each element using the UnaryOperator
. This demonstrates that you can perform more complex operations inside your lambda expression.