English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input Output (I/O)

Java Reader/Writer

Java Other Topics

Java ArrayList replaceAll() Usage and Example

Java ArrayList Methods

The Java ArrayList replaceAll() method replaces each element of the arraylist with the result specified by the parameter.

The syntax of replaceAll() method is:

arraylist.replaceAll(UnaryOperator<E> operator)

replaceAll() parameters

  • operator -Operation applied to each element

replaceAll() return value

The replaceAll() method does not return any value. Instead, it replaces all values in the arraylist with the value of the operator.

Example1:Change all elements to uppercase

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        //Create an ArrayList
        ArrayList<String> languages = new ArrayList<>();
        // Add elements to ArrayList
        languages.add("java");
        languages.add("javascript");
        languages.add("swift");
        languages.add("python");
        System.out.println("ArrayList: ") + languages);
        // Replace all elements with uppercase
        languages.replaceAll(e -> e.toUpperCase());
        System.out.println("Updated ArrayList: ") + languages);
    }
}

Output Result

ArrayList: [java, javascript, swift, python]
Updated ArrayList: [JAVA, JAVASCRIPT, SWIFT, PYTHON]

In the above example, we created an ArrayList named language. Note this line,

languages.replaceAll(e -> e.toUpperCase());

Here,

  • e -> e.toUpperCase()  -  It is a lambda expression. It converts all elements of the arraylist to uppercase. For more information, please visitJava Lambda Expressions.

  • replaceAll() - Replace all elements of arraylist with uppercase.

Example2:Multiply all elements of ArrayList2

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        //Create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        // Add elements to ArrayList
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("ArrayList: ") + numbers);
        //The2Multiplies all elements of the hashmap
        numbers.replaceAll(e -> e * 2);;
        System.out.println("Updated ArrayList: ") + numbers);
    }
}

Output Result

ArrayList: [1, 2, 3]
Updated ArrayList: [2, 4, 6]

In the above example, we created an ArrayList named numbers. Note this line,

numbers.replaceAll(e -> e * 2);

Here,

  • e -> e * 2 - Multiplies each element of the ArrayList by2

  • replaceAll() - using e -> e * 2The result replaces all elements of the ArrayList

NoteWe can also use the Collections.replace() method to perform a specified replacement operation in Java.

Java ArrayList Methods