Java Collections nCopies() Method

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

Definition

In Java, Collections.nCopies(n, value) returns a List that contains value exactly n times. The list is immutable: you cannot add, remove, or replace elements.

It is very cheap, because it does not store n elements; it stores the value once and the count. Its most common use is as a starting point: new ArrayList<>(Collections.nCopies(5, 0)) gives a changeable list of five zeros.

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

Syntax

Pass the count and the value to repeat.

Java
List<String> dashes = Collections.nCopies(3, "-");          // [-, -, -]
List<Integer> zeros = new ArrayList<>(Collections.nCopies(4, 0));  // changeable [0, 0, 0, 0]
String line = String.join("", Collections.nCopies(5, "="));       // "====="
CallWhat it does
Collections.nCopies(n, v)Immutable list of n times v.
new ArrayList<>(Collections.nCopies(n, v))A changeable list starting with n copies.
String.join("", Collections.nCopies(n, s))Repeats a string (s.repeat(n) in Java 11+).
Collections.nCopies(0, v)An empty list.

All positions refer to the same object. That is harmless for immutable values such as String and Integer, but surprising for mutable objects, as Example 3 shows.

Library declarations
Java
// java.util.Collections
public static <T> List<T> nCopies(int n, T o)

Parameters

ParameterDescription
nHow many copies the list contains. Must be 0 or more.
oThe element to repeat. May be null.

Return Value

Return ValueDescription
List<T>An immutable list of size n in which every element is o.

Compare with Similar Methods

Other ways to build a list of repeated or fixed values:

MethodHow it differs
fill(list, v)Overwrites an existing list; cannot create elements.
singletonList(v)An immutable list with exactly one element.
emptyList()An immutable list with no elements.
Arrays.asList(...)A fixed-size list of different values you write out.

Java Examples

Example 1 — Repeat a Word and Build a Divider

Use nCopies to repeat a word, draw a divider line, and make an empty list.

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

public class CollectionsNCopiesRepeat {
    public static void main(String[] args) {
        List<String> laugh = Collections.nCopies(3, "ha");
        System.out.println(laugh + " size " + laugh.size());
        System.out.println(String.join("", laugh));

        System.out.println(String.join("", Collections.nCopies(10, "-")));
        System.out.println(Collections.nCopies(0, "x"));
    }
}

How It Works

  1. A tiny list. nCopies(3, "ha") behaves like a three-element list and prints like one.
  2. Join to a string. String.join("", laugh) glues the copies together with no separator: "hahaha".
  3. Divider. Ten copies of "-" joined make a ten-character line, a trick that works on Java 8 before String.repeat existed.
  4. Zero copies. A count of 0 is allowed and gives an empty list.

Example 2 — Turn nCopies into an Editable List

Copy the immutable list into an ArrayList, then edit one slot. Also see what happens on the original and with a negative count.

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

public class CollectionsNCopiesMutable {
    public static void main(String[] args) {
        List<Integer> fixed = Collections.nCopies(5, 0);
        List<Integer> board = new ArrayList<>(fixed);
        board.set(2, 9);
        System.out.println("Editable copy: " + board);

        try {
            fixed.set(2, 9);
        } catch (UnsupportedOperationException e) {
            System.out.println("Original: " + e.getClass().getSimpleName());
        }

        try {
            Collections.nCopies(-1, 0);
        } catch (IllegalArgumentException e) {
            System.out.println("n = -1: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Copy constructor. new ArrayList<>(fixed) stores five real elements that can be replaced.
  2. Edit the copy. Setting index 2 to 9 only affects board.
  3. Immutable original. fixed.set(2, 9) throws UnsupportedOperationException, because the nCopies list cannot change.
  4. Negative count. A list cannot have -1 elements, so nCopies(-1, 0) throws IllegalArgumentException.

Example 3 — Every Copy Is the Same Object

Fill a list with "copies" of one StringBuilder, change the first one, and watch all of them change.

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

public class CollectionsNCopiesShared {
    public static void main(String[] args) {
        List<StringBuilder> boxes = new ArrayList<>(Collections.nCopies(3, new StringBuilder()));
        boxes.get(0).append("x");
        System.out.println("Shared:   " + boxes);
        System.out.println("Same object: " + (boxes.get(0) == boxes.get(2)));

        List<StringBuilder> separate = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            separate.add(new StringBuilder());
        }
        separate.get(0).append("x");
        System.out.println("Separate: " + separate);
    }
}

How It Works

  1. One builder, three slots. new StringBuilder() runs once; nCopies repeats that single reference three times.
  2. Change one, see three. Appending "x" through index 0 modifies the only builder, so every slot prints x.
  3. Identity check. == compares references, and slots 0 and 2 point to the same object.
  4. The fix. Creating a new builder inside a loop gives three independent objects; only the first holds "x", and the other two print as empty strings.

Frequently Asked Questions

An immutable List of size n in which every element is the given object.
new ArrayList<>(Collections.nCopies(n, 0)). The ArrayList copy can be changed freely.
No. The list is immutable, so add, set, and remove throw UnsupportedOperationException.
nCopies throws IllegalArgumentException.
No. Every position refers to the same object, which matters if the object is mutable.

Next: newSetFromMap()

Build a Set backed by any Map, such as a ConcurrentHashMap.

newSetFromMap() 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.

12 people found this page helpful