In Java, Collections.replaceAll(list, oldVal, newVal) finds every element of a List that is equal to oldVal and replaces it with newVal. It returns true if at least one element was replaced, and false if oldVal was not found.
Do not confuse it with the List instance method list.replaceAll(UnaryOperator) (Java 8+), which runs a function on every element. The Collections version swaps one specific value for another.
A static method of the java.util.Collections utility class, available since Java 1.4. Call it on the class name, as in Collections.replaceAll(...), after import java.util.Collections;. You never create a Collections object.
Syntax
Pass the list, the value to look for, and its replacement.
Java
List<String> words = new ArrayList<>(Arrays.asList("cat", "dog", "cat"));
boolean changed = Collections.replaceAll(words, "cat", "fox"); // true
// words = [fox, dog, fox]
Collections.replaceAll(words, "cow", "ox"); // false
Call
What it does
Collections.replaceAll(list, a, b)
Every element equal to a becomes b.
Collections.replaceAll(list, null, b)
Every null becomes b.
list.replaceAll(x -> f(x))
The List method: transforms every element.
list.set(list.indexOf(a), b)
Replaces only the first match (and fails if there is none).
Matching uses equals, or == null when oldVal is null, so it is null-safe. The method scans the whole list once, in linear time.
Library declarations
Java
// java.util.Collections
public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal)
Parameters
Parameter
Description
list
The list to update. It must support set.
oldVal
The value to find. May be null.
newVal
The value to put in its place. May be null if the list allows it.
Return Value
Return Value
Description
true
At least one element was equal to oldVal and was replaced.
Two matches. "colour" appears at indexes 0 and 2, and both are replaced in a single call.
Other words skipped. "flavor" and "honour" are not equal to "colour", so they stay as they are.
A miss returns false. "grey" is not in the list, so the second call changes nothing and reports false.
Example 2 — Replace null Placeholders
Form fields that were left blank are stored as null; replace them with a dash for display.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsReplaceAllNull {
public static void main(String[] args) {
List<String> fields = new ArrayList<>(Arrays.asList("Ana", null, "Delhi", null));
int blanks = Collections.frequency(fields, null);
Collections.replaceAll(fields, null, "-");
System.out.println("Filled " + blanks + " blanks: " + fields);
}
}
Output
Filled 2 blanks: [Ana, -, Delhi, -]
How It Works
Count first.frequency(fields, null) counts the two null entries before anything changes.
Null-safe matching. Because oldVal is null, the method compares with == instead of calling equals on null.
Result. Both blanks become "-" while the real values stay in place.
Example 3 — Collections.replaceAll vs list.replaceAll
Replace one value with the helper method, then transform every element with the List method.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsReplaceAllVsList {
public static void main(String[] args) {
List<String> levels = new ArrayList<>(Arrays.asList("low", "mid", "low"));
Collections.replaceAll(levels, "low", "LOW");
System.out.println("One value: " + levels);
levels.replaceAll(s -> s + "!");
System.out.println("Every element: " + levels);
}
}
Output
One value: [LOW, mid, LOW]
Every element: [LOW!, mid!, LOW!]
How It Works
Value swap. The static method changes only elements equal to "low", leaving "mid" alone.
Function on all.levels.replaceAll(s -> s + "!") is the Java 8 List method; the lambda is a UnaryOperator<String> applied to each element.
Different signatures. The helper takes three arguments and returns a boolean; the List method takes one function and returns nothing.
Frequently Asked Questions
true if at least one element equal to oldVal was replaced, false if there was no match.
No. Collections.replaceAll(list, oldVal, newVal) swaps one value for another. list.replaceAll(operator), added in Java 8, applies a function to every element.
Yes. Pass null as oldVal, and every null element is replaced with newVal.
It uses equals, or a null check when oldVal is null. Your element class needs a correct equals method for custom objects.
The method only returns true or false. Call Collections.frequency(list, oldVal) before replacing to get the count.