Collections copy() method in Java with Examples - GeeksforGeeks

15 May, 2019
The copy() method of java.util.Collections class is used to copy 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.
This method runs in linear time.
Syntax:
public static void copy(List dest, List src)
Parameters: This method takes the following argument as a parameter
dest – The destination list.
src – The source list.
Exception: This method throws IndexOutOfBoundsException, if the destination list is too small to contain the entire source List.
Below are the examples to illustrate the checkedSortedSet() method
Example 1:
// Java program to demonstrate// copy() method  import java.util.*;  public class GFG1 {    public static void main(String[] args)        throws Exception    {        try {              // creating object of Source list and destination List            List<String> srclst = new ArrayList<String>(3);            List<String> destlst = new ArrayList<String>(3);              // Adding element to srclst            srclst.add("Ram");            srclst.add("Gopal");            srclst.add("Verma");              // Adding element to destlst            destlst.add("1");            destlst.add("2");            destlst.add("3");              // printing the srclst            System.out.println("Value of source list: " + srclst);              // printing the destlst            System.out.println("Value of destination list: " + destlst);              System.out.println("\nAfter copying:\n");              // copy element into destlst            Collections.copy(destlst, srclst);              // printing the srclst            System.out.println("Value of source list: " + srclst);              // printing the destlst            System.out.println("Value of destination list: " + destlst);        }          catch (IllegalArgumentException e) {            System.out.println("Exception thrown : " + e);        }          catch (IndexOutOfBoundsException e) {            System.out.println("Exception thrown : " + e);        }    }}
Value of source list: [Ram, Gopal, Verma]
Value of destination list: [1, 2, 3]

After copying:

Value of source list: [Ram, Gopal, Verma]
Value of destination list: [Ram, Gopal, Verma]

Example 2: For IndexOutOfBoundsException
// Java program to demonstrate// copy() method  import java.util.*;  public class GFG1 {    public static void main(String[] argv) throws Exception    {        try {              // creating object of Source list and destination List            List<String> srclst = new ArrayList<String>(3);            List<String> destlst = new ArrayList<String>(2);              // Adding element to srclst            srclst.add("Ram");            srclst.add("Gopal");            srclst.add("Verma");              // Adding element to destlst            destlst.add("1");            destlst.add("2");              // printing the srclst            System.out.println("Value of source list: " + srclst);              // printing the destlst            System.out.println("Value of destination list: " + destlst);              System.out.println("\nAfter copying:\n");              // copy element into destlst            Collections.copy(destlst, srclst);              // printing the srclst            System.out.println("Value of source list: " + srclst);              // printing the destlst            System.out.println("Value of destination list: " + destlst);        }          catch (IllegalArgumentException e) {            System.out.println("Exception thrown : " + e);        }          catch (IndexOutOfBoundsException e) {            System.out.println("Exception thrown : " + e);        }    }}
Value of source list: [Ram, Gopal, Verma]
Value of destination list: [1, 2]

After copying:

Exception thrown : 
java.lang.IndexOutOfBoundsException:
 Source does not fit in dest

Akanksha_Rai
Java - util package
Java-Collections
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Interfaces in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Set in Java
Multithreading in Java
Collections in Java
Queue Interface In Java