Home Java LinkedList Methods replaceAll() Java LinkedList replaceAll() Method Definition In Java, replaceAll(operator) goes through the LinkedList from start to end, passes each element to the UnaryOperator, and stores the result back in the same position. The list is changed in place.
A UnaryOperator<E> is a function that takes one value and returns a value of the same type, usually written as a lambda such as s -> s.toUpperCase() or as a method reference such as String::trim.
An instance method of java.util.LinkedList (since Java 8): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by List, and LinkedList implements it. replaceAll() was added in Java 8.
Syntax Pass a lambda or a method reference.
LinkedList<String> list = new LinkedList<>(Arrays.asList("a", "b", "c"));
list.replaceAll(s -> s.toUpperCase()); // [A, B, C]
list.replaceAll(String::toLowerCase); // [a, b, c] Call What it does list.replaceAll(x -> expr)Replaces every element with the value of expr. list.replaceAll(String::trim)A method reference as the operator. list.set(i, e)Replaces one element at a time. list.forEach(action)Visits elements but does not replace them.
The operator must return a value of the same type as the elements. A List<Integer> cannot be turned into strings with this method.
Library declarations // java.util.LinkedList (default method from java.util.List)
public void replaceAll(UnaryOperator<E> operator) Parameters Parameter Description operator (UnaryOperator<E>)The function applied to every element. It must not be null.
Return Value Return Value Description voidNothing is returned. The list itself is updated.
Important
This method needs Java 8 or later. A null operator throws NullPointerException. If the list holds null elements and the operator calls a method on them, such as s.toUpperCase(), you get a NullPointerException as well. The size never changes: replaceAll cannot add or remove elements.
Compare with Similar Methods Related ways to process every element:
Method How it differs set(i, e)Replaces a single element by index. forEach(action)Runs code for every element but does not store results back. sort(c)Reorders the elements instead of changing their values. ArrayList.replaceAll()The same default method on an array-backed list.
Java Examples 1. Uppercase every element 2. Update numbers 3. Null cases Example 1 — Transform Every String in a List Change all the names to upper case, then trim and change them back with method references.
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListReplaceAllDemo {
public static void main(String[] args) {
LinkedList<String> names = new LinkedList<>(
Arrays.asList(" ana ", "ben", " cleo"));
names.replaceAll(String::trim);
System.out.println("trimmed: " + names);
names.replaceAll(s -> s.toUpperCase());
System.out.println("upper: " + names);
names.replaceAll(s -> s.substring(0, 1));
System.out.println("initials: " + names);
}
} trimmed: [ana, ben, cleo]
upper: [ANA, BEN, CLEO]
initials: [A, B, C] How It Works Messy input. Two names have extra spaces around them.A method reference. String::trim is applied to each element, which removes the spaces.A lambda. s -> s.toUpperCase() turns each name into capitals, and the same list object keeps the result.One more pass. The last call keeps only the first letter of each name, so the list ends as A, B, C with the same size of three.Example 2 — Apply a Price Increase to Every Value Add a fixed amount to every price and double every quantity with replaceAll.
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListReplaceAllNumbers {
public static void main(String[] args) {
LinkedList<Integer> prices = new LinkedList<>(Arrays.asList(100, 250, 80));
prices.replaceAll(p -> p + 15);
System.out.println("plus 15: " + prices);
prices.replaceAll(p -> p * 2);
System.out.println("doubled: " + prices);
int total = 0;
for (int p : prices) {
total += p;
}
System.out.println("total: " + total);
}
} plus 15: [115, 265, 95]
doubled: [230, 530, 190]
total: 950 How It Works Three prices. The list starts as 100, 250 and 80.First lambda adds 15. Each value becomes 115, 265 and 95, in the same positions.Second lambda doubles. The updated values are doubled to 230, 530 and 190.A normal loop sums them. The total of the final values is 950.Example 3 — See the Two Null Problems in replaceAll Pass a null operator, and then run an operator on a list that contains a null element.
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListReplaceAllErrors {
public static void main(String[] args) {
LinkedList<String> words = new LinkedList<>(Arrays.asList("one", null, "three"));
try {
words.replaceAll(null);
} catch (NullPointerException e) {
System.out.println("null operator -> NullPointerException");
}
try {
words.replaceAll(w -> w.toUpperCase());
} catch (NullPointerException e) {
System.out.println("null element -> NullPointerException");
}
System.out.println("words: " + words);
}
} null operator -> NullPointerException
null element -> NullPointerException
words: [ONE, null, three] How It Works A list with a null element. The middle element is null.A null operator. Java has no function to apply, so it throws NullPointerException at once.A null element. The lambda calls toUpperCase() on null when it reaches the second element, which throws.Earlier work is kept. The first element was already replaced by "ONE" before the failure, so the final list is partly updated.Frequently Asked Questions What does LinkedList replaceAll() do? It replaces every element with the result of applying a UnaryOperator to it, and updates the list in place.
Which Java version added replaceAll()? Java 8, together with lambdas and the UnaryOperator interface.
Can replaceAll() change the type of the elements? No. The operator must return the same type as the elements. To change the type, build a new list, for example with streams.
Does replaceAll() change the size of the list? No. It only replaces values, so the size stays the same.
What is the difference between replaceAll() and forEach()? replaceAll stores the result of the function back into the list. forEach only runs code for each element and discards any result.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful
Helpful Share Copy link Suggestion