Java Collection Tutorial - Java Collections.copy(List <? super T> dest, List <? extends T> src)








Syntax

Collections.copy(List <? super T> dest, List <? extends T> src) has the following syntax.

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

Example

In the following code shows how to use Collections.copy(List <? super T> dest, List <? extends T> src) method.

//  w  ww.ja  va  2s  .  com
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
   public static void main(String args[]) {
   
      List<String>  srclst = new ArrayList<String> (5);
      List<String>  destlst = new ArrayList<String> (10);
      
      // populate two lists
      srclst.add("tutorial");
      srclst.add("from");
      srclst.add("java2s.com");
      
      destlst.add("Java Tutorial");
      destlst.add("From");
      destlst.add("Java2s.com");
      
      
      // copy into dest list
      Collections.copy(destlst, srclst);            
      
      System.out.println("Value of source list: "+srclst);
      System.out.println("Value of destination list: "+destlst);
   }    
}

The code above generates the following result.