java.util.Collections.copy() Method
Advertisements
Description
The copy(List<? super T>, List<? extends T>) method is used to copy all of the elements from one list into another.
Declaration
Following is the declaration for java.util.Collections.copy() method.
public static <T> void copy(List<? super T> dest,List<? extends T> src)
Parameters
dest--This is the destination list.
src--This is the source list.
Return Value
NA
Exception
IndexOutOfBoundsException--This is thrown if the destination list is too small to contain the entire source List.
UnsupportedOperationException--This is thrown if the destination list's list-iterator does not support the set operation.
Example
The following example shows the usage of java.util.Collections.copy()
package com.tutorialspoint;
import java.util.*;
public class CollectionsDemo {
public static void main(String args[]) {
// create two lists
List<String> srclst = new ArrayList<String>(5);
List<String> destlst = new ArrayList<String>(10);
// populate two lists
srclst.add("Java");
srclst.add("is");
srclst.add("best");
destlst.add("C++");
destlst.add("is");
destlst.add("older");
// copy into dest list
Collections.copy(destlst, srclst);
System.out.println("Value of source list: "+srclst);
System.out.println("Value of destination list: "+destlst);
}
}
Let us compile and run the above program, this will produce the following result.
Value of source list: [Java, is, best] Value of destination list: [Java, is, best]