Collections.fill replaces every element of a list with the same value in place, which is a quick way to reset a fixed-size list without recreating it.
List<String> slots = new ArrayList<>(List.of("a", "b", "c", "d"));
Collections.fill(slots, "empty");
System.out.println("Filled: " + slots);
Filled: [empty, empty, empty, empty]
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27