Java List replaceAll() Method Tutorial

In this section, we will learn what the List replaceAll() method is and how to use it in Java.

What is Java List replaceAll() Method?

The Java List replaceAll() method is used to modify or replace the entire elements of a list object by running a method on every element of that list.

Basically, first you define a method that takes each element of a list object and then inside that method are the instructions that apply to each element in order to modify it and create a new value as a result. This method is then passed as a reference to the replaceAll() method and it will be called for every element in the target list object.

In short, this method is a way of updating the elements of a list object by running a method on its elements.

Java List replaceAll() Method Syntax:

default void replaceAll(UnaryOperator<E> operator)

replaceAll() Method Parameters:

This method takes one argument and basically that is a reference to a method that:

  • Firstly, it takes one argument of the same type as the elements in the target object list.
  • Secondly, its return data type is the same as the type of the elements in the target object list.

So you can create this method using lambda expression and pass it as the argument of the replaceAll() method or use a reference to a currently existed static or instance method.

replaceAll() Method Return Value:

The return value of the replaceAll() method is void. But the method that we pass as its argument, returns a set of values that are of the same type as the elements of the target list object and these returned values are the ones that will replace the old elements of the target list object.

replaceAll() Method Exceptions:

This method might throw two types of exceptions:

UnsupportedOperationException: You’ll get this exception if this method is not supported in the target object list. Or if the elements of the target list object are not replaceable.

NullPointerException: we get this exception if we pass a `null` value as the argument of this method.

Example: using List replaceAll() method

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

public class Main {

    public static void main(String[] args){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        
        list.replaceAll(e->{
         return e*10;
        });

        for (int i :list){
         System.out.println(i);
        }
    }
}

Output:

10

20

30

40

50

60

 

In this example, we’ve created a lambda method and passed it as the argument of the replaceAll() method.

In the body of this lambda method, the instructions are to change the incoming values by multiplying them to 10 and return the result.

So now, because this method will be executed for each and every element of the target list object, all the elements are replaced by their multiplication to 10.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies