Copy Elements from Java ArrayList to other Java ArrayList

Problem Statement:

How to Copy Elements from Java ArrayList to other Java ArrayList?

Copy Elements from Java ArrayList to other Java ArrayList

Copy Elements from Java ArrayList to other Java ArrayList

Description:

Step1:  Constructs an empty list studentList1 with an initial capacity of ten.

Step2:  Constructs an empty list studentList2 with an initial capacity of ten.

Step3:  Use  Collections.copy(studentList2, studentList1);

  • Copies all of the student names elements from one student array list into studentList2.
  • After the copy operation, the index of each copied element (studentList1) in the destination  list (studentList2) will be identical to its index in the source list. Basically it overwrites the elements in the destination list which has the same index in the source list.
  • The destination list which in this example is studentList2 must be at least as long as the source list studentList1.
  • If the destination list studentList2 is longer, the remaining elements in the destination list studentList2 are unaffected.
  • Copy method runs in linear time.

Copy Elements from Java ArrayList to other Java ArrayList

Sample Code to Copy Elements from Java ArrayList to other Java ArrayList

package com.stixs.java.collections;

import java.util.ArrayList;
import java.util.Collections;

/**
 * The Class STXCopyArrayListElementsToOtherArrayList will demonstrate how to
 * copy ArrayList elements - student names studentLlist1 into another Java
 * ArrayList studentList2. This example uses copy() of Collection class - which
 * Copy Elements of ArrayList to Java ArrayList Example.
 */
public class STXCopyArrayListElementsToOtherArrayList {

 public static void main(String[] args) {

 // Constructs an empty studentList1 with an initial capacity of ten.
 ArrayList<String> studentList1 = new ArrayList<String>();

 // Add String elements to Arraylist
 studentList1.add("John");
 studentList1.add("Eric");
 studentList1.add("Rob");

 // Constructs an empty studentList2 with an initial capacity of ten.
 ArrayList<String> studentList2 = new ArrayList<String>();

 // Add elements to Arraylist
 studentList2.add("Allan");
 studentList2.add("Black");
 studentList2.add("Den");
 studentList2.add("Eian");
 studentList2.add("Porta");
 studentList2.add("Anuva");

 System.out.println("STXCopyArrayListElementsToOtherArrayList:: Student names in the "
 + "studentList2 before copy method is invoked : " + studentList2);

 /*
 * Copies all of the student names elements from one student array list
 * into studentList2.
 * 
 * After the copy operation, the index of each copied element
 * (studentList1) in the destination list (studentList2) will be
 * identical to its index in the source list. Basically it overwrites
 * the elements in the destination list which has the same index in teh
 * source list.
 * 
 * The destination list which in this example is studentList2 must be at
 * least as long as the source list studentList.
 * 
 * If the destination list studentList2 is longer, the remaining
 * elements in the destination list studentList2 are unaffected.
 * 
 * Copy method runs in linear time.
 */

 Collections.copy(studentList2, studentList1);

 /*
 * Note that If studentList2 is not long enough to hold all elements of
 * studentList1, the copy method throws IndexOutOfBoundsException.
 */

 System.out.println("STXCopyArrayListElementsToOtherArrayList:: Student names in the "
 + "studentList2 after copy method is invoked : " + studentList2);
 }
}

seperator

Output:

STXCopyArrayListElementsToOtherArrayList:: Student names in the studentList2 before copy 
method is invoked : [Allan, Black, Den, Eian, Porta, Anuva]
STXCopyArrayListElementsToOtherArrayList:: Student names in the studentList2 after copy 
method is invoked : [John, Eric, Rob, Eian, Porta, Anuva]

seperator

Java API Classes and Methods used to Copy Elements from Java ArrayList to other Java ArrayList

Collections.copy()

Collection Copy

seperator

Reference

Refer below path to get the complete details about Java Collections class.

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html

seperator

You may also like this

Copy Elements from Java ArrayList to other Java ArrayList

About TechnoStixs.com

Copy Elements from Java ArrayList to other Java ArrayList

TechnoStixs.com is a dedicated weblog for Java/J2EE and web developers. We take pride in our work. Every publication is carefully analyzed, written and tested to ease the understanding of the topic or subject.

We cover Java Core Technologies, eCommerce, J2EE Frameworks, Web Service, Build Tools, Unit Test Frameworks and Others.

1,288 total views, 1 views today

Add comment