Java Array Deep Copy Example

Learn to create deep copy of an array in Java with example. We will be using Apache Commons Lang library and it’s utility method SerializationUtils.clone( array ) to array deep copy.

Read More : Shallow copy of an array

1. Deep copying of an array

A deep copy of an array will have all objects and it’s referred objects – created fresh. References from old array will not point to any object in deep copied array, and similarly references from copied array will not point to any object in original old array.

Any change at any level of object hierarchy of deep copied array – will not change the state of any object in original array.

1.1. Import Apache Commons Lang

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.7</version>
</dependency>

1.2. Using SerializationUtils

Now use SerializationUtils.clone() method to create a deep copy of given array.

package com.howtodoinjava.demo;

import org.apache.commons.lang3.SerializationUtils;

public class Main 
{
	public static void main(String[] args) 
	{
		Employee[] empArr = new Employee[2];	//Original array

		empArr[0] = new Employee(100, "Lokesh", "Gupta", new Department(1, "HR"));
		empArr[1] = new Employee(200, "Pankaj", "Kumar", new Department(2, "Finance"));
		
		Employee[] copiedArray = SerializationUtils.clone(empArr);	//Deep copied array
		
		//Change state of original array
		empArr[0].setFirstName("Unknown");
		empArr[0].getDepartment().setName("Unknown");
		
		//Verify the change in original array - CHANGED
		System.out.println(empArr[0].getFirstName());					//Unknown
		System.out.println(empArr[0].getDepartment().getName());		//Unknown

		//Verify the change in deep copied array - UNCHANGED
		System.out.println(copiedArray[0].getFirstName());				//Lokesh
		System.out.println(copiedArray[0].getDepartment().getName());	//HR
	}
}

Only condition to use SerializationUtils is that classes stored in array must be Serializable.

package com.howtodoinjava.demo;

import java.io.Serializable;

public class Employee implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String firstName;
	private String lastName;
	private Department department;

	public Employee(int id, String fName, String lName, Department department) {
		super();
		this.id = id;
		this.firstName = fName;
		this.lastName = lName;
		this.department = department;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}
}
package com.howtodoinjava.demo;

import java.io.Serializable;

public class Department implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	Integer id;
	String name;
	
	public Department(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + "]";
	}
}

Happy Learning !!

References:

SerializationUtils Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.