Here we are going to share different ways through which you can clone ArrayList to another ArrayList in Java.

Cloning ArrayList to another ArrayList using Collections.copy(para1, para2):

You can copy the elements of the ArrayList to another ArrayList by using Collections.copy(para1, para2). In this method, you will pass both lists. The first parameter will be the list in which we will copy the elements and the second parameter will be the list from which we will copy the elements.

Source code:

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

public class ArrayListClone {
    
    public static void main(String[] args) {
        //initializing two array lists
        List list1 = new ArrayList<>();
        List list2 = new ArrayList<>();
        
        list1.add("Pizza");
        list1.add("Burger");
        list1.add("Pasta");
        list1.add("Steak");
        
        list2.add("");
        list2.add("");
        list2.add("");
        list2.add("");
        //Copying elements of list 1 to list 2
        Collections.copy(list2, list1);
        System.out.println("Elements copied successfully to list 2!!");
        System.out.println("List 2: "+list2);
    }
}

Output:

run:
Elements copied successfully to list 2!!
List 2: [Pizza, Burger, Pasta, Steak]
BUILD SUCCESSFUL (total time: 0 seconds)

Cloning the ArrayList to another ArrayList by manually copying the elements:

For this process, we will simply use a loop to copy the elements from ArrayList to another ArrayList.

Source code:

import java.util.ArrayList;
import java.util.List;

public class ArrayListClone {
    
    public static List CopyingElements(List list){
        List list2 = new ArrayList<>();
        
        for(int i=0; i<list.size(); i++){
            list2.add(list.get(i));
        }
        
        return list2;
    }
    public static void main(String[] args) {
        //initializing two array lists
        List list1 = new ArrayList<>();
        
        list1.add("Sara");
        list1.add("John");
        list1.add("Sam");
        list1.add("Khan");
        
        List list2= CopyingElements(list1);
        //Copying elements of list 1 to list 2
        System.out.println("Elements copied successfully to list 2!!");
        System.out.println("List 2: "+list2);
    }
}

Output:

run:
Elements copied successfully to list 2!!
List 2: [Sara, John, Sam, Khan]
BUILD SUCCESSFUL (total time: 1 second)

Using clone():

If you want to create a clone of ArrayList in fewer lines of code, then use the clone() method.

Source code:

import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListClone {

    public static void main(String[] args) {
        ArrayList list1 = new ArrayList(Arrays.asList("John", "Sara", "Ali", "Sam"));
        ArrayList list2 = (ArrayList) list1.clone();

        //Display of both ArrayLists
        System.out.println("Original ArrayList: " + list1);
        System.out.println("Cloned ArrayList: " + list2);
    }
}

Output:

run:
Original ArrayList: [John, Sara, Ali, Sam]
Cloned ArrayList: [John, Sara, Ali, Sam]
BUILD SUCCESSFUL (total time: 0 seconds)

References:

Leave A Comment