Collections: Collections.fill overwrites every slot with one value

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.

Code
List<String> slots = new ArrayList<>(List.of("a", "b", "c", "d"));
Collections.fill(slots, "empty");
System.out.println("Filled: " + slots);
Output
Filled: [empty, empty, empty, empty]
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms