How to Make a Deep Copy of Java ArrayList? Last Updated : 08 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The Advantage of ArrayList is it can be defined without giving a predefined size. But the disadvantage is it is more expensive to create and maintain. To have the solution for these expenses we can create a deep copy of an ArrayList. There are two types of copies that can be made the first one is a deep copy and the second one is a shallow copy. In this article, we will learn to make a deep copy of Java ArrayList in Java. PrerequisiteShallow Copy vs Deep CopyJava ArrayListDeep Copy of ArrayListUnlike shallow copy in deep copy, we entirely make a new ArrayList without referring to the old ArrayList. In this, instead of duplicating the ArrayList it also copies the objects in it. When we make a deep copy we entirely make a new ArrayList at the new address and then copy the elements one by one. Therefore if we make any changes in one list it doesn't get reflected in the second ArrayList. Following is the example of code in which we will make a deep copy of an ArrayList in Java. Java // Java Program to demonstrate Deep Copy of an ArrayList import java.util.ArrayList; public class DeepCopyExample { // Method to create a deep copy of an ArrayList private static ArrayList<String> deepCopy(ArrayList<String> originalList) { // Create a new ArrayList to store the copied // elements ArrayList<String> copiedList = new ArrayList<>(); // Iterate over each element in the original list for (String item : originalList) { // Creating a new instance of each element copiedList.add(new String(item)); } // Return the deep copied list return copiedList; } public static void main(String[] args) { // Creating the original ArrayList ArrayList<String> originalList = new ArrayList<>(); // Add string "1" to the list originalList.add("1"); // Add string "2" to the list originalList.add("2"); // Add string "3" to the list originalList.add("3"); // Creating a deep copy of the ArrayList ArrayList<String> copiedList = deepCopy(originalList); // Modify the copied list // Add string "4" to the copied list copiedList.add("4"); // Print both the original and copied lists // Print original list System.out.println("Original List: " + originalList); // Print copied list System.out.println("Copied List: " + copiedList); } } Output:Original List: [1,2,3]Copied List: [1,2,3,4]Explaination of the above Program:A class method DeepCopy replicates each element of an ArrayList into a new instance, creating a truly independent copy.Starting with "1", "2", and "3", the program generates a deep copy of the list.Element "4" is added only to the copy, leaving the original unchanged.Both lists are displayed, revealing the separation. Comment More infoAdvertise with us Next Article How to Make a Deep Copy of Java ArrayList? B bhushanc2003 Follow Improve Article Tags : Java Java Programs Java-ArrayList Java Examples Practice Tags : Java Similar Reads Deep Copy of 2D Array in Java In Java, performing a deep copy of a 2D array containing objects requires careful consideration to ensure independence between the original and copied arrays. By creating new instances of objects for each element in the array, developers can achieve a true deep copy, preventing unintended side effec 3 min read Copy Elements of Vector to Java ArrayList Since Vector class and ArrayList class both are part of Java Collections, ie Collection framework, so both of these classes can use methods available to the Collection framework. Copy() method is one of the methods of Collection Interface which is used to copy one list to another list, here list can 3 min read How to make ArrayList Thread-Safe in Java? In Java, Thread is the smallest unit of execution within the program. It represents an independent path of execution that can run concurrently with other threads. When dealing with multi-threaded applications, where multiple threads are accessing and modifying data concurrently, it's crucial to ensu 3 min read How to Convert TreeMap to an ArrayList in Java? TreeMap is a part of the Java Collection framework. Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order. Java TreeMap contains only unique elements. It cannot 4 min read How to Convert ArrayList to LinkedHashSet in Java? ArrayList is a data structure that overcomes the shortcomings of the common array in Java wherein the size has to be explicitly specified beforehand. The length of the array data structure cannot be modified which is taken care of the ArrayList data structure. This data structure is also known as th 8 min read Like