The copy() method of the java.util.Collections class copies all elements from a source list to a destination list. Each element is copied to the same index in the destination list. The destination list must already contain at least as many elements as the source list; otherwise, an IndexOutOfBoundsException is thrown.
- Destination list must be at least the same size as the source list.
- Existing elements in the destination list are overwritten.
- Runs in O(n) time, where n is the number of elements copied.
import java.util.*;
public class GFG {
public static void main(String[] args) {
List<Integer> source = Arrays.asList(10, 20, 30);
List<Integer> destination =
new ArrayList<>(Arrays.asList(0, 0, 0));
Collections.copy(destination, source);
System.out.println(destination);
}
}
Output
[10, 20, 30]
Syntax
public static <T> void copy(List<? super T> dest, List<? extends T> src)
Parameters
- dest: The destination list where elements are copied.
- src: The source list from which elements are copied
Return Value: This method does not return any value.
Exception: IndexOutOfBoundsException: Thrown if the destination list contains fewer elements than the source list.
Example: Copying Elements Between Two Lists
import java.util.*;
public class GFG {
public static void main(String[] args) {
List<String> source = new ArrayList<>();
source.add("Ram");
source.add("Gopal");
source.add("Verma");
List<String> destination =
new ArrayList<>(Arrays.asList("1", "2", "3"));
System.out.println("Source List: " + source);
System.out.println("Destination List: " + destination);
Collections.copy(destination, source);
System.out.println("\nAfter Copying:");
System.out.println("Source List: " + source);
System.out.println("Destination List: " + destination);
}
}
Output
Source List: [Ram, Gopal, Verma] Destination List: [1, 2, 3] After Copying: Source List: [Ram, Gopal, Verma] Destination List: [Ram, Gopal, Verma]
Explanation: This example copies all elements from the source list to the destination list. Since both lists have the same size, the copy operation succeeds and replaces the destination elements.
Example: Destination List Too Small
import java.util.*;
public class GFG {
public static void main(String[] args) {
List<String> source = Arrays.asList(
"Ram", "Gopal", "Verma");
List<String> destination =
new ArrayList<>(Arrays.asList("1", "2"));
System.out.println("Source List: " + source);
System.out.println("Destination List: " + destination);
try {
Collections.copy(destination, source);
}
catch (IndexOutOfBoundsException e) {
System.out.println("\nException: " + e);
}
}
}
Output
Source List: [Ram, Gopal, Verma] Destination List: [1, 2] Exception: java.lang.IndexOutOfBoundsException: Source does not fit in dest
Explanation: Since the destination list contains fewer elements than the source list, Collections.copy() throws an IndexOutOfBoundsException.
Difference Between Collections.copy() and List.addAll()
| Feature | Collections.copy() | List.addAll() |
|---|---|---|
| Purpose | Replaces existing elements | Appends new elements |
| Destination Size | Must already contain enough elements | Can be empty |
| Existing Elements | Overwritten | Preserved and new elements added |
| Return Type | void | boolean |
Advantages
- Copies elements while preserving their positions.
- Easy way to replace elements in an existing list.
- Works with any implementation of the
Listinterface. - Efficient with linear time complexity.