Java LinkedList replaceAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
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]
CallWhat 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
// java.util.LinkedList (default method from java.util.List)
public void replaceAll(UnaryOperator<E> operator)

Parameters

ParameterDescription
operator (UnaryOperator<E>)The function applied to every element. It must not be null.

Return Value

Return ValueDescription
voidNothing is returned. The list itself is updated.

Compare with Similar Methods

Related ways to process every element:

MethodHow 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

Example 1 — Transform Every String in a List

Change all the names to upper case, then trim and change them back with method references.

Java
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);
    }
}

How It Works

  1. Messy input. Two names have extra spaces around them.
  2. A method reference. String::trim is applied to each element, which removes the spaces.
  3. A lambda. s -> s.toUpperCase() turns each name into capitals, and the same list object keeps the result.
  4. 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.

Java
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);
    }
}

How It Works

  1. Three prices. The list starts as 100, 250 and 80.
  2. First lambda adds 15. Each value becomes 115, 265 and 95, in the same positions.
  3. Second lambda doubles. The updated values are doubled to 230, 530 and 190.
  4. 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.

Java
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);
    }
}

How It Works

  1. A list with a null element. The middle element is null.
  2. A null operator. Java has no function to apply, so it throws NullPointerException at once.
  3. A null element. The lambda calls toUpperCase() on null when it reaches the second element, which throws.
  4. 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

It replaces every element with the result of applying a UnaryOperator to it, and updates the list in place.
Java 8, together with lambdas and the UnaryOperator interface.
No. The operator must return the same type as the elements. To change the type, build a new list, for example with streams.
No. It only replaces values, so the size stays the same.
replaceAll stores the result of the function back into the list. forEach only runs code for each element and discards any result.

Next: retainAll()

Keep only the elements that also appear in another collection.

retainAll() 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.

7 people found this page helpful