How to Deep Copy Arraylist in Java

In this article, we are going to learn deep copy ArrayList in Java.

There are mainly two concepts Shallow copy and deep copy.

When an object gets copied via reference not actual values then it is called Shallow copy. In it, changes made to an object will also reflect to another object. It means if we change elements of one object then the other object will also be changed.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

In deep copy, the copied object is completely independent and changes made to it do not reflect to the original object.
Let’s understand with the examples.

Example of Deep Copy ArrayList

Here, we are copying one ArrayList elements to ther using addAll() method and see changes made to second list does not modify original list.

Output:

[India, US, China] [India, US, China] [India, Russia, China]

Note: This method will only work if ArrayList contains primitive data types or immutable collection. In case, ArrayList contains custom objects, then we need to explicitly clone the custom objects.

Deep Copy using Clone() Method

We can also use clone() method to create a copy of ArrayList but this method create swallow copy.

Let’s see with the help of example.

Create main class named CloneArrayListMain.java

Output

———–Original List———–
John 102
David 105
———–Copied List———-
John 102
David 105

As you can see, changes to clonedStudentList also got reflected in studentList.

To create a true deep copy of ArrayList, we should create a new ArrayList and copy all the cloned elements to new ArrayList one by one and we should also clone Student object properly.

To create deep copy of Student class, we can divide its class members to mutable and immutable types.

  • Immutable fields( String data types): We can directly use immutable fields in cloned object. Immutable fields include wrapper classes, String and primitive types.
  • Mutable fields(Date data type): We should create new object for the mutable attribute and then assign it to cloned object.

Here is the correct clone method for Student class.

As you can see, we have used super.clone() to clone the Student object and then set dateOfBirth explicitly to clonedStudent as Date is mutable field.

Here is code to create deep copy of ArrayList by copying cloned elements one by one to new ArrayList.

Complete code to deep copy ArrayList in java

Here is complete java program to create deep copy of ArrayList in java.

As you can see, changes made to clonedStudentList did not reflect in original ArrayList studentList.

That’s all about how to deep copy ArrayList in java.


import_contacts

You may also like:

Related Posts

  • 02 October

    Initialize ArrayList with values in Java

    In this article, we will learn to initialize ArrayList with values in Java. ArrayList is an implementation class of List interface in Java. It is used to store elements. It is based on a dynamic array concept that grows accordingly. We can Initialize ArrayList with values in several ways. Let’s see some of them with […]

  • 17 March

    PriorityQueue in Java 8

    In this post, we will see about Java 8 PriorityQueue. When the objects are supposed to be processed on the basis of their priority, in that scenario we use PriorityQueue. It’s a special type of queue (also, unbound queues) where the elements can be ordered either as per their natural ordering or based on a […]

  • 06 October

    2d Arraylist java example

    In this post, we will see how to create 2d Arraylist in java. Best way to create 2d Arraylist is to create list of list in java. [crayon-60921c56173c2435129899/] Let’s create a program to implement 2d Arraylist java. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? […]

  • 04 May

    How HashMap works in java

    Most common interview questions are <code>How HashMap works in java</code>, “How get and put method of HashMap work internally”. Here I am trying to explain internal functionality with an easy example. [crayon-60921c5637f06758837279-i/]  is one of the most used Collections in java.Rather than going through theory, we will start with example first, so that you will […]

  • 21 October

    How to Sort HashSet in Java

    In this post, we will see how to sort HashSet in java. HashSet is a collection which does not store elements in any order. You might come across a situation where you need to sort HashSet. There can be many ways to sort HashSet, we will see two methods here. 💻 Awesome Tech Resources: Looking […]

  • 20 October

    Java Array to Set

    In this post, we will learn java array to set conversion. There are many ways to convert array to set. 1. Using Java 8’s Stream If you are using Java 8, I would recommend using this method. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.