Java Collections replaceAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

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
CallWhat 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

ParameterDescription
listThe list to update. It must support set.
oldValThe value to find. May be null.
newValThe value to put in its place. May be null if the list allows it.

Return Value

Return ValueDescription
trueAt least one element was equal to oldVal and was replaced.
falseNo element matched; the list is unchanged.

Compare with Similar Methods

Ways to change values inside a list:

MethodHow it differs
list.replaceAll(op)Runs a UnaryOperator on every element.
fill(list, v)Replaces every element, matching or not.
frequency(list, v)Counts how many elements would be replaced.
list.set(i, v)Replaces one element by index.

Java Examples

Example 1 — Fix Every Misspelling at Once

Swap British spellings for American ones and check the return value of each call.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsReplaceAllSpelling {
    public static void main(String[] args) {
        List<String> words = new ArrayList<>(Arrays.asList("colour", "flavor", "colour", "honour"));

        boolean first = Collections.replaceAll(words, "colour", "color");
        System.out.println(first + " " + words);

        boolean second = Collections.replaceAll(words, "grey", "gray");
        System.out.println(second + " " + words);
    }
}

How It Works

  1. Two matches. "colour" appears at indexes 0 and 2, and both are replaced in a single call.
  2. Other words skipped. "flavor" and "honour" are not equal to "colour", so they stay as they are.
  3. 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);
    }
}

How It Works

  1. Count first. frequency(fields, null) counts the two null entries before anything changes.
  2. Null-safe matching. Because oldVal is null, the method compares with == instead of calling equals on null.
  3. 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);
    }
}

How It Works

  1. Value swap. The static method changes only elements equal to "low", leaving "mid" alone.
  2. Function on all. levels.replaceAll(s -> s + "!") is the Java 8 List method; the lambda is a UnaryOperator<String> applied to each element.
  3. 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.

Next: reverse()

Reverse the order of the elements in a list.

reverse() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful