Java Collections fill() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.fill(list, value) replaces every existing element of a List with the same value. The size of the list does not change, and the method returns nothing.

The key word is existing: fill overwrites positions that are already there. It never adds elements, so filling an empty list leaves it empty. To create a list of repeated values, use nCopies.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.fill(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Pass the list and the value to write into every position.

Java
List<Integer> scores = new ArrayList<>(Arrays.asList(12, 7, 30));
Collections.fill(scores, 0);                   // [0, 0, 0]
Collections.fill(scores.subList(0, 2), 5);     // [5, 5, 0]
CallWhat it does
Collections.fill(list, v)Every element becomes v.
Collections.fill(list.subList(a, b), v)Only indexes a to b - 1 change.
Collections.fill(Arrays.asList(arr), v)Fills an object array through its list view.
Collections.fill(list, null)Clears values but keeps the size.

fill runs in linear time. Every position receives the same reference, so filling with a mutable object (such as a StringBuilder) means all positions share one object.

Library declarations
Java
// java.util.Collections
public static <T> void fill(List<? super T> list, T obj)

Parameters

ParameterDescription
listThe list whose elements are replaced. It must support set.
objThe value to store in every position. May be null if the list allows it.

Return Value

Return ValueDescription
voidNothing. Every element of the list is now obj.

Compare with Similar Methods

Ways to set many elements at once:

MethodHow it differs
nCopies(n, v)Creates a new list of n copies; works when you have no list yet.
replaceAll(list, a, b)Replaces only elements equal to a.
Arrays.fill(arr, v)The array version, including primitive arrays.
list.replaceAll(f)Computes a new value for each element with a function.

Java Examples

Example 1 — Reset All Scores to Zero

At the start of a new round, set every player's score back to 0.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsFillReset {
    public static void main(String[] args) {
        List<Integer> scores = new ArrayList<>(Arrays.asList(12, 7, 30, 18));
        System.out.println("Round 1: " + scores);

        Collections.fill(scores, 0);
        System.out.println("Round 2: " + scores + " (size " + scores.size() + ")");
    }
}

How It Works

  1. Four scores. The list holds one score per player.
  2. Overwrite in place. fill(scores, 0) calls set on each index with the value 0.
  3. Size kept. No players are removed: the list still has four entries, all zero.

Example 2 — fill() on an Empty List vs an Array View

A new ArrayList with a capacity is still empty, so fill has nothing to overwrite. A list view of a new array does have slots.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsFillEmptyTrap {
    public static void main(String[] args) {
        List<String> seats = new ArrayList<>(4);
        Collections.fill(seats, "free");
        System.out.println("ArrayList(4): " + seats + " size " + seats.size());

        String[] slots = new String[4];
        Collections.fill(Arrays.asList(slots), "free");
        System.out.println("Array view:   " + Arrays.toString(slots));
    }
}

How It Works

  1. Capacity is not size. new ArrayList<>(4) only reserves room; the list has zero elements.
  2. Nothing to replace. fill loops over existing positions, of which there are none, so the list stays [].
  3. An array has slots. new String[4] holds four null slots, and its Arrays.asList view has size 4.
  4. Written into the array. Filling the view stores "free" in each slot of slots itself.

Example 3 — Fill Part of a List, and an Immutable List

Paint a run of pixels with subList, then try to fill the read-only list from nCopies.

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsFillRange {
    public static void main(String[] args) {
        List<String> pixels = new ArrayList<>(Collections.nCopies(6, "."));
        Collections.fill(pixels.subList(2, 5), "#");
        System.out.println(String.join("", pixels));

        List<String> fixed = Collections.nCopies(3, "x");
        try {
            Collections.fill(fixed, "y");
        } catch (UnsupportedOperationException e) {
            System.out.println("nCopies list: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Changeable copy. Six dots come from nCopies, wrapped in new ArrayList<>(...) so the list can be edited.
  2. A window of three. subList(2, 5) covers indexes 2, 3, and 4, and only those become #.
  3. Joined for display. String.join("", pixels) prints the row as ..###..
  4. Immutable source. The list returned directly by nCopies cannot be changed, so filling it throws UnsupportedOperationException.

Frequently Asked Questions

It replaces every element of a List with the same value. The size does not change.
new ArrayList<>(n) creates an empty list with room for n elements. fill only overwrites existing elements, so nothing happens. Use new ArrayList<>(Collections.nCopies(n, value)) instead.
Yes. Fill a subList view: Collections.fill(list.subList(from, to), value).
Collections.fill works on a List. Arrays.fill works on arrays, including primitive arrays such as int[].
No. Every position gets the same reference. With mutable objects, changing one position changes them all.

Next: frequency()

Count how many elements equal a given value.

frequency() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful