.replaceAll()

MattiasGradin's avatar
Published Jun 24, 2024
Contribute to Docs

The .replaceAll() method in Java is used for transforming all elements of a list according to a specified unary operation, effectively updating each element in place with the result of the operation applied to it. It does not return any value.

Syntax

arrayList.replaceAll(UnaryOperator<E> operator)
  • arrayList: The ArrayList on which the .replaceAll() method is called.
  • operator: Represents the unary operator that will be applied to each element in the ArrayList.

Example

In this example, an ArrayList named cities is created and populated with city names using .add(). Then, .replaceAll() is used to transform all city names to lowercase, updating each element in place.

import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// Create an ArrayList for different cities
ArrayList<String> cities = new ArrayList<>();
// Add cities to the ArrayList
cities.add("STOCKHOLM");
cities.add("LONDON");
cities.add("PARIS");
cities.add("BERLIN");
System.out.println("Cities in uppercase: " + cities);
// Call replaceAll() on ArrayList to make every String element lowercase
cities.replaceAll(String::toLowerCase);
System.out.println("Updated cities in lowercase: " + cities);
}
}

The output for this code would look like this:

Cities in uppercase: [STOCKHOLM, LONDON, PARIS, BERLIN]
Updated cities in lowercase: [stockholm, london, paris, berlin]

All contributors

Contribute to Docs

Learn Java on Codecademy