Java Collections fill() Method
Example
Replace all items in a list with "Mazda":
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
Collections.fill(cars, "Mazda");
System.out.println(cars);
}
}
Definition and Usage
The fill() method replaces every item in a list with the given value.
The size of the list does not change.
Syntax
public static void fill(List<T> list, T item)
T refers to the data type of items in the list.
Parameter Values
| Parameter | Description |
|---|---|
| list | Required. The list to fill. |
| item | Required. The value to put in every place of the list. |
Technical Details
| Returns: | No return value. |
|---|
Related Pages
Java Collections Framework Tutorial
❮ Collections Methods