Home Java Collections Methods nCopies() Java Collections nCopies() Method 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.
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, "=")); // "=====" Call What 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.util.Collections
public static <T> List<T> nCopies(int n, T o) Parameters Parameter Description nHow many copies the list contains. Must be 0 or more. oThe element to repeat. May be null.
Return Value Return Value Description List<T>An immutable list of size n in which every element is o.
Important
A negative n throws IllegalArgumentException. Any attempt to change the returned list (add, set, remove, or Collections.fill) throws UnsupportedOperationException. Copy it into an ArrayList when you need to edit.
Compare with Similar Methods Other ways to build a list of repeated or fixed values:
Java Examples 1. Repeat a value 2. Make it editable 3. Shared object trap Example 1 — Repeat a Word and Build a Divider Use nCopies to repeat a word, draw a divider line, and make an empty list.
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"));
}
} [ha, ha, ha] size 3
hahaha
----------
[] How It Works A tiny list. nCopies(3, "ha") behaves like a three-element list and prints like one.Join to a string. String.join("", laugh) glues the copies together with no separator: "hahaha".Divider. Ten copies of "-" joined make a ten-character line, a trick that works on Java 8 before String.repeat existed.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.
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());
}
}
} Editable copy: [0, 0, 9, 0, 0]
Original: UnsupportedOperationException
n = -1: IllegalArgumentException How It Works Copy constructor. new ArrayList<>(fixed) stores five real elements that can be replaced.Edit the copy. Setting index 2 to 9 only affects board.Immutable original. fixed.set(2, 9) throws UnsupportedOperationException, because the nCopies list cannot change.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.
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);
}
} Shared: [x, x, x]
Same object: true
Separate: [x, , ] How It Works One builder, three slots. new StringBuilder() runs once; nCopies repeats that single reference three times.Change one, see three. Appending "x" through index 0 modifies the only builder, so every slot prints x.Identity check. == compares references, and slots 0 and 2 point to the same object.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 What does Collections.nCopies() return? An immutable List of size n in which every element is the given object.
How do I create a modifiable list of n zeros? new ArrayList<>(Collections.nCopies(n, 0)). The ArrayList copy can be changed freely.
Can I add elements to a list from nCopies()? No. The list is immutable, so add, set, and remove throw UnsupportedOperationException.
What happens if n is negative? nCopies throws IllegalArgumentException.
Does nCopies() copy the object n times? No. Every position refers to the same object, which matters if the object is mutable.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion