List.replaceAll()
Replaces every list element with the result of a function.
Available since: Java 8
Syntax
default void replaceAll(UnaryOperator<E> operator)Parameters
| Parameter | Type | Description |
|---|---|---|
| operator | UnaryOperator<E> | A function that receives an element and returns its new value. |
Examples
All strings to upper case:
void main() {
List<String> tags = new ArrayList<>(List.of("java", "sql", "git"));
tags.replaceAll(String::toUpperCase);
IO.println(tags);
}Output:
[JAVA, SQL, GIT]Notes
It changes the list itself rather than creating a new one. The element type stays the same — you cannot turn a List<String> into a List<Integer> this way; use stream().map() for that.
See also
- List.set() — Replaces the list element at an index.
- Stream.map() — Transforms every element of a stream.
- UnaryOperator — A function whose argument and result have the same type.
Related recipes
In the tutorial
- Lists: List and ArrayList — Lists in Java: ArrayList, adding, reading and removing elements, unmodifiable List.of lists and lists of numbers.