Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaCopy Elements of One ArrayList to Another ArrayList in Java

Copy Elements of One ArrayList to Another ArrayList in Java

It is the implementation class of List Interface. It allows duplicated objects/elements and as well as maintains the insertion order. You can get the element present inside the ArrayList by the index of it now you need to pass it into the getting (index) method. You can add the elements into ArrayList using the add() method.

Syntax: ArrayList Initialization  

ArrayList<Integer> gfg=new ArrayList<>();

Copying Elements of one ArrayList to another ArrayList

There are two approaches first you actually just need to pass the reference of one ArrayList to another and in this case, if you change in one ArrayList value or element then you can see the same change in another ArrayList. The second approach is where you will create actual duplicates means if you change in one ArrayList Element then it will not reflect in the other one.

Approach 1: Using the assignment operator(=)

In this approach, we will simply assign the first ArrayList reference to the second but there is one important aspect to look at here we did not create a new object we simply pointed the second ArrayList to the first one. So if you make a change in the first ArrayList it will reflect in the second one also because you are using the same object. We can also change one value of one ArrayList and can look for the same in the other one whether it is changed or not.

Syntax:

ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=gfg;

Below is the implementation of the above problem statement.

Java




// Java Program for copying one ArrayList to another
 
import java.io.*;
import java.util.ArrayList;
 
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList<Integer> gfg = new ArrayList<>();
 
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
 
        // Assigning the first reference to second
        ArrayList<Integer> gfg2 = gfg;
 
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
        for (Integer value : gfg2) {
            System.out.println(value);
        }
 
        // here we changed the third element to 23
        // we changed in second list and you can
        // see the same change in the first Arraylist
        gfg2.set(2, 23);
 
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}


Output

-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =23
third element of second list =23

Approach 2: Passing in the constructor

In this approach, we will simply pass the first ArrayList in the second ArrayList’s constructor. By using this approach if we change one ArrayList element/value it will not affect the other one, so this is the approach where we actually created the duplicates. We can also change one value of one ArrayList and can look for the same in the other one whether it is changed or not.

Syntax :

ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=new ArrayList<>(gfg);

Below is the implementation of the above problem statement:

Java




// Java Program for  copying one ArrayList to another
 
import java.io.*;
import java.util.ArrayList;
 
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList<Integer> gfg = new ArrayList<>();
 
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
 
        // passing in the constructor
        ArrayList<Integer> gfg2 = new ArrayList<>(gfg);
 
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
        for (Integer value : gfg2) {
            System.out.println(value);
        }
 
        // here we changed the third element to 23
        // we changed in second list and you can
        // here we will not see the same change in the first
        gfg2.set(2, 23);
 
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}


Output

-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =22
third element of second list =23

Approach 3: Adding one by one using add() method

In this approach, we will iterate over each element of the first ArrayList and add that element in the second ArrayList. Here if you change first ArrayList element then it will not change the elements of the second ArrayList. We can also change one value of one ArrayList and can look for the same in the other one whether it is changed or not.

Syntax :

ArrayList<Integer> gfg=new ArrayList<>();
ArrayList<Integer> gfg2=new ArrayList<>();
for(Integer val: gfg){
gfg2.add(val);
}

Below is the implementation of the above problem statement:

Java




// Java Program for  copying one ArrayList to another
 
import java.io.*;
import java.util.ArrayList;
 
class GFG {
    public static void main(String[] args)
    {
        // creation of ArrayList of Integers
        ArrayList<Integer> gfg = new ArrayList<>();
 
        // adding elements to  first ArrayList
        gfg.add(10);
        gfg.add(21);
        gfg.add(22);
        gfg.add(35);
 
        ArrayList<Integer> gfg2 = new ArrayList<>();
 
        // adding element to the second ArrayList
        // by iterating over one by one
        for (Integer value : gfg) {
            gfg2.add(value);
        }
 
        // Iterating over  second ArrayList
        System.out.println(
            "-----Iterating over the second ArrayList----");
 
        for (Integer value : gfg2) {
            System.out.println(value);
        }
 
        // here we changed the third element to 23
        // we changed in second list
        // here we will not see the same change in the first
        gfg2.set(2, 23);
 
        System.out.println("third element of first list ="
                           + gfg.get(2));
        System.out.println("third element of second list ="
                           + gfg2.get(2));
    }
}


Output

-----Iterating over the second ArrayList----
10
21
22
35
third element of first list =22
third element of second list =23

Approach 4: Using addAll() method

The addAll() method is used to add all the elements from one ArrayList to another ArrayList. For this implementation, we have to import the package java.util.*. 

Step 1: Declare the ArrayList 1 and add the values to it.

Step 2: Create another ArrayList 2 with the same type.

Step 3: Now, simply add the values from one ArrayList to another by using the method addAll(). Specify ArrayList2.addAll(ArrayList1).

Step 4: Now, print the ArrayList 2.

Java




import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        ArrayList<String> AL1 = new ArrayList<>();
        AL1.add("geeks");
        AL1.add("forgeeks");
        AL1.add("learning");
        AL1.add("platform");
 
        ArrayList<String> AL2 = new ArrayList<>();
        AL2.addAll(AL1);
        System.out.println("Original ArrayList : " + AL1);
        System.out.println("Copied ArrayList : " + AL2);
    }
}


Output

Original ArrayList : [geeks, forgeeks, learning, platform]
Copied ArrayList : [geeks, forgeeks, learning, platform]

Approach 5 : Using List.copyOf() method 

List.copyOf() method is used to add the elements of one ArrayList to another. To use this method, we have to import the package java.util.List.* or java.util.* . It is a static factory method.

Step 1: Declare the ArrayList 1 and add the values to it.

Step 2: Create another ArrayList 2 with the same type.

Step 3: Now, simply add the values from one ArrayList to another by using the method List.copyOf(). Specify List.copyOf(ArrayList1) in the constructor of newly created ArrayList 2.

Step 4: Now, print the ArrayList 2.

Java




import java.util.*;
 
class GFG {
    public static void main(String[] args)
    {
        ArrayList<String> AL1 = new ArrayList<>();
        AL1.add("geeks");
        AL1.add("forgeeks");
        AL1.add("learning");
        AL1.add("platform");
 
        ArrayList<String> AL2
            = new ArrayList<>(List.copyOf(AL1));
 
        System.out.println("Original ArrayList : " + AL1);
        System.out.println("Copied Arraylist : " + AL2);
    }
}


Output

Original ArrayList : [geeks, forgeeks, learning, platform]
Copied Arraylist : [geeks, forgeeks, learning, platform]

Time complexity: O(N) where N is the size of ArrayList

Auxiliary Space: O(N)

Approach 6 : Using clone() method

In java, clone() method is used to copy an entire object values without any side effects to the objects.

Step 1: Declare the ArrayList 1 and add the values to it.

Step 2: Create another ArrayList 2 with the same type.

Step 3: Now, simply add the values from one ArrayList to another by using the method object.clone(). Specify reference type before the clone() method for type casting.

Step 4: Now, print the ArrayList 2.

Java




/*package whatever //do not write package name here */
 
import java.util.*;
import java.io.*;
 
class GFG {
    public static void main (String[] args) {
       ArrayList<String> AL1 = new ArrayList<>();
       AL1.add("geeks");
       AL1.add("forgeeks");
       AL1.add("learning");
       AL1.add("platform");
 
       ArrayList<String> AL2= new ArrayList<>();
       AL2 = (ArrayList)AL1.clone();
       System.out.println("Original ArrayList : " + AL1);
       System.out.println("Copied Arraylist : " + AL2);
    }
}


Output :

Original ArrayList : [geeks, forgeeks, learning, platform]

Copied Arraylist : [geeks, forgeeks, learning, platform]

RELATED ARTICLES

Most Popular

Recent Comments