Collections copy() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai