Java Collections copy() Method



Description

The Java Collections 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.

Copying a List of Integers Example

The following example shows the usage of Java Collection copy(List,List) method to copy a list of Integers. We've created two lists with some integers. Using copy(List,List) method, we're copying content of one list to another while overriding the existing content.

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<Integer> srclst = new ArrayList<>();
      List<Integer> destlst = new ArrayList<>();

      // populate two lists
      srclst.add(1);
      srclst.add(2);
      srclst.add(3);

      destlst.add(4);
      destlst.add(5);
      destlst.add(6);
      destlst.add(7);

      // copy into dest list
      Collections.copy(destlst, srclst);            

      System.out.println("Value of source list: "+srclst);
      System.out.println("Value of destination list: "+destlst);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Value of source list: [1, 2, 3]
Value of destination list: [1, 2, 3, 7]

Copying a List of Strings Example

The following example shows the usage of Java Collection copy(List,List) method to copy a list of strings. We've created two lists with some strings. Using copy(List,List) method, we're copying content of one list to another while overriding the existing content.

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<String> srclst = new ArrayList<>();
      List<String> destlst = new ArrayList<>();

      // populate two lists
      srclst.add("A");
      srclst.add("B");
      srclst.add("C");

      destlst.add("D");
      destlst.add("E");
      destlst.add("F");
      destlst.add("G");

      // copy into dest list
      Collections.copy(destlst, srclst);            

      System.out.println("Value of source list: "+srclst);
      System.out.println("Value of destination list: "+destlst);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Value of source list: [A, B, C]
Value of destination list: [A, B, C, G]

Copying a List of Objects Example

The following example shows the usage of Java Collection copy(List,List) method to copy a list of Student objects. We've created two lists with some Student objects. Using copy(List,List) method, we're copying content of one list to another while overriding the existing content.

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<Student> srclst = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));
      List<Student> destlst = new ArrayList<>(Arrays.asList(new Student(4, "Jene"),
         new Student(5, "Julie"), new Student(6, "Trus")));

      // copy into dest list
      Collections.copy(destlst, srclst);            

      System.out.println("Value of source list: "+srclst);
      System.out.println("Value of destination list: "+destlst);
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Value of source list: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Value of destination list: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_collections.htm
Advertisements