Java Collections copy() Method

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

Definition

In Java, Collections.copy(dest, src) copies every element of src into dest, starting at index 0. Element i of the source overwrites element i of the destination. The method returns nothing.

It does not make a new list and does not grow the destination. The destination must already have at least as many elements as the source. Any extra elements at the end of the destination are left unchanged.

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

Syntax

Pass the destination first and the source second.

Java
List<String> dest = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
List<String> src = Arrays.asList("X", "Y");
Collections.copy(dest, src);    // dest = [X, Y, c, d]
CallWhat it does
Collections.copy(dest, src)Overwrites the first src.size() elements of dest.
Collections.copy(dest.subList(k, k + n), src)Copies to an offset k.
new ArrayList<>(src)The usual way to make a brand-new copy.
dest.addAll(src)Appends instead of overwriting.

The copy is shallow: both lists end up holding references to the same element objects. The order of the arguments follows the pattern "destination, source", the same as System.arraycopy in spirit.

Library declarations
Java
// java.util.Collections
public static <T> void copy(List<? super T> dest, List<? extends T> src)

Parameters

ParameterDescription
destThe list to copy into. Its size must be at least src.size(), and it must support set.
srcThe list to copy from. It is not changed.

Return Value

Return ValueDescription
voidNothing. The first elements of dest now match src.

Compare with Similar Methods

Ways to copy elements between lists:

MethodHow it differs
new ArrayList<>(src)Creates a new list of the right size; no size rule to worry about.
dest.addAll(src)Appends to the end, growing the list.
fill(list, v)Writes one value everywhere instead of copying a list.
Arrays.copyOf(arr, n)Copies arrays and creates the new array for you.

Java Examples

Example 1 — Copy a Short List into a Longer One

New top-two results overwrite the first two entries of a five-entry board; the rest stays.

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

public class CollectionsCopyOverwrite {
    public static void main(String[] args) {
        List<String> board = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e"));
        List<String> winners = Arrays.asList("X", "Y");

        Collections.copy(board, winners);

        System.out.println("board:   " + board);
        System.out.println("winners: " + winners);
    }
}

How It Works

  1. Size check passes. The board has 5 elements and the source has 2, so the source fits.
  2. Index by index. X overwrites index 0 and Y overwrites index 1.
  3. Tail untouched. c, d, and e are beyond the copied range and keep their values; the source list is not modified.

Example 2 — "Source does not fit in dest" and Two Fixes

Copying into a new ArrayList with only a capacity fails. Pre-filling the destination, or simply using the copy constructor, works.

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

public class CollectionsCopyFit {
    public static void main(String[] args) {
        List<String> src = Arrays.asList("red", "green", "blue");

        List<String> dest = new ArrayList<>(3);
        try {
            Collections.copy(dest, src);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }

        List<String> padded = new ArrayList<>(Collections.nCopies(src.size(), ""));
        Collections.copy(padded, src);
        System.out.println("Pre-filled: " + padded);

        List<String> simple = new ArrayList<>(src);
        System.out.println("Copy constructor: " + simple);
    }
}

How It Works

  1. Room but no elements. new ArrayList<>(3) has capacity 3 but size 0, which is smaller than the source size of 3.
  2. Checked up front. copy compares the two sizes before copying anything and throws IndexOutOfBoundsException with the message shown.
  3. Fix one. Filling the destination with three empty strings gives it size 3, so the copy then overwrites them.
  4. Fix two. When you just want a duplicate list, new ArrayList<>(src) is shorter and cannot fail this way.

Example 3 — Copy into the Middle of an Array

Use a subList of an array view as the destination so the letters land at index 1.

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

public class CollectionsCopyOffset {
    public static void main(String[] args) {
        String[] slots = {"-", "-", "-", "-", "-", "-"};
        List<String> view = Arrays.asList(slots);
        List<String> word = Arrays.asList("J", "A", "V", "A");

        Collections.copy(view.subList(1, 5), word);

        System.out.println(String.join("", slots));
    }
}

How It Works

  1. Array view as the target. Arrays.asList(slots) writes through to the array, and it supports set even though its size is fixed.
  2. Shifting the start. view.subList(1, 5) is a four-element window starting at index 1, so its index 0 is the array's index 1.
  3. Exact fit. The window and the word both have four elements, so J, A, V, A fill indexes 1 to 4 and the dashes at both ends remain.

Frequently Asked Questions

It copies the elements of the source list into the destination list, starting at index 0, overwriting what was there.
The destination list has fewer elements than the source. A new ArrayList with a capacity still has size 0, so it does not count.
No. It writes into an existing list. To create a new copy, use new ArrayList<>(source).
They are left unchanged. Only the first source.size() positions are overwritten.
No. It copies references, so both lists share the same element objects.

Next: disjoint()

Check whether two collections have no element in common.

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

10 people found this page helpful