How do I use List.replaceAll() method?

The List.replaceAll() method was introduced in Java 8. This method replaces each element of the list with the result of applying the operator to that element. The operator or function you pass to replaceAll() should be a UnaryOperator.

Here is a simple example:

package org.kodejava.util;

import java.util.ArrayList;
import java.util.List;
import java.util.function.UnaryOperator;

public class ListReplaceAllExample {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);

        // Define an UnaryOperator to square each number
        UnaryOperator<Integer> square = n -> n * n;

        // Use replaceAll() method to square each number in the list
        numbers.replaceAll(square);

        System.out.println(numbers);
    }
}

Outputs:

[1, 4, 9, 16, 25]

In this example, the UnaryOperator square squares each element. The List.replaceAll() method applies this operator to all elements in the list.

Note that replaceAll() modifies the original list and does not return a new list. Please also be aware that this operation is in-place and hence modifies the original List. If you want to keep the original List unchanged, create a new List and add elements to it after applying the function.

The primary purpose of the List.replaceAll() method in Java is to perform an in-place transformation of all elements within a list based on a given unary function or operation.

A Unary function or operation is one that takes a single input and produces a result. In the context of replaceAll(), the unary operation is typically provided as a lambda expression or method reference which is applied to each element in the list in turn.

If successful, replaceAll() modifies the list such that each original element has been replaced by the result of applying the provided unary operation to that element. This operation is performed on the original list, and no new list is created, making it an efficient option for transforming large lists.

Here is an example which doubles each integer in a list:

package org.kodejava.util;

import java.util.ArrayList;
import java.util.List;

public class ListReplaceAllSecondExample {
    public static void main(String[] args) {
        List<Integer> ints = new ArrayList<>();

        ints.add(1);
        ints.add(2);
        ints.add(3);

        // Double every integer in the List
        ints.replaceAll(n -> n * 2);

        System.out.println(ints); 
    }
}

Outputs:

[2, 4, 6]

In conclusion, List.replaceAll() provides a convenient and efficient way to modify all elements in a list according to a specified operation or function. It’s especially useful when using the Streams API and functional programming techniques introduced in Java 8.

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.