Collections

Copy Collection to another Collection example

With this example we are going to demonstrate how to copy a Collection to another Collection. In particular, we will use an ArrayList to be copied to a Vector, but the same API applies to any type of List implementation classes e.g. LinkedList etc. In short, to copy a list to another list you should:

  • Create an ArrayList.
  • Populate the arrayList with elements, with add(E e) API method of ArrayList.
  • Create a new Vector.
  • Populate the vector with elements, with the add(E e) API method of the Vector.
  • Invoke the copy(List dstList, List sourceList) API method of the Collections. It copies all elements of the source list to the destination list, overriding any element of the destination list that resides at the same index position as the one from the source list. The destination list must be long enough to hold all copied elements of the source list otherwise IndexOutOfBoundsException is thrown.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
 
public class CopyListToList {
 
  public static void main(String[] args) {

    /*

Please note that the same API applies to any type of 

List implementation classes e.g. LinkedList etc
     */

    // Create an ArrayList and populate it with elements
    ArrayList arrayList = new ArrayList();
    arrayList.add("arl_element_1");
    arrayList.add("arl_element_4");
    arrayList.add("arl_element_2");
    arrayList.add("arl_element_5");
    arrayList.add("arl_element_3");
 
    // Create a Vector and populate it with elements
    Vector vector = new Vector();
    vector.add("vec_element_1");
    vector.add("vec_element_6");
    vector.add("vec_element_7");
    vector.add("vec_element_4");
    vector.add("vec_element_2");
    vector.add("vec_element_5");
    vector.add("vec_element_3");
 
    System.out.println("Vector Contains : " + vector);

    /*

static void copy(List dstList, List sourceList) method copies all elements 

of source list to destination list overriding any element of the destination 

list that resides at the same index position as the one from the source list. 

The destination list must be long enough to hold all copied elements of the 

source list otherwise IndexOutOfBoundsException is thrown
     */
    Collections.copy(vector,arrayList);
 
    System.out.println("Vector elements after copy : " + vector);   
  }
}

Output:

Vector Contains : [vec_element_1, vec_element_6, vec_element_7, vec_element_4, vec_element_2, vec_element_5, vec_element_3]
Vector elements after copy : [arl_element_1, arl_element_4, arl_element_2, arl_element_5, arl_element_3, vec_element_5, vec_element_3]

 
This was an example of how to copy a Collection to another Collection in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button