Close

Java Collections - Collections.copy() Examples

Java Collections Java Java API 


Class:

java.util.Collections

java.lang.Objectjava.lang.Objectjava.util.Collectionsjava.util.CollectionsLogicBig

Method:

public static <T> void copy(List<? super T> dest,
                            List<? extends T> src)

Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.


Examples


package com.logicbig.example.collections;

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

public class CopyExample {

public static void main(String... args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
List<Integer> list2 = Arrays.asList(10, 20, 30, 40, 50, 60);

Collections.copy(list2, list);
System.out.println(list2);

}
}

Output

[1, 2, 3, 4, 50, 60]




See Also