Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java ArrayList Clone

This Java ArrayList clone example shows how to clone Java ArrayList object.
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3.  
  4. //Java clone ArrayList example
  5.  
  6. public class JavaCloneArrayListExample {
  7.    
  8.     public static void main(String args[]){
  9.        
  10.         //create original ArrayList object
  11.         ArrayList<Employee> aListOriginal = new ArrayList<Employee>();
  12.         aListOriginal.add(new Employee("Mark", 10000));
  13.         aListOriginal.add(new Employee("Steve", 20000));
  14.         aListOriginal.add(new Employee("Alex", 30000));
  15.        
  16.         //to clone ArrayList use clone method
  17.         ArrayList<Employee> aListCloned = (ArrayList<Employee>)aListOriginal.clone();
  18.        
  19.         /*
  20.          * NOTE: clone method only shallow copies the ArrayList. That means that
  21.          * only ArrayList object reference is cloned not the objects within the
  22.          * original ArrayList.
  23.          *
  24.          * Cloned ArrayList still refers to the same original objects added to the
  25.          * original ArrayList. That also means that if we change any of the object
  26.          * contained within cloned ArrayList, object within original ArrayList also
  27.          * changes!!
  28.          */
  29.        
  30.         //Change the object in original ArrayList
  31.         aListCloned.get(0).setName("Name Updated");
  32.         System.out.println("Original ArrayList " + aListOriginal);
  33.         System.out.println("Cloned ArrayList " + aListCloned);
  34.        
  35.         /*
  36.          * As you can see from the output, clone method only copies the ArrayList
  37.          * reference and not the objects contained in the ArrayList.
  38.          *
  39.          * To deep copy ArrayList we also need to clone the objects contained in
  40.          * the ArrayList using iterator as given below.
  41.          */
  42.        
  43.         ArrayList<Employee> aListNew = new ArrayList<Employee>();
  44.         Iterator<Employee> itr = aListOriginal.iterator();
  45.        
  46.         while(itr.hasNext()){
  47.            
  48.             //add cloned Employee object to cloned ArrayList instead of original object.
  49.            
  50.             //In order for this to work, Class Employee must have clone method.
  51.             aListNew.add((Employee)itr.next().clone());
  52.         }
  53.        
  54.         System.out.println("ArrayList cloned - Deep copied");
  55.        
  56.         //now change again object of Original ArrayList
  57.         aListOriginal.get(0).setName("Mark");
  58.        
  59.         System.out.println("Original ArrayList " + aListOriginal);
  60.         System.out.println("New ArrayList " + aListNew);
  61.        
  62.         /*
  63.          * As you can see from the output now, changing object of original
  64.          * ArrayList does not affect the object in the cloned ArrayList.
  65.          */
  66.        
  67.     }
  68. }
  69.  
  70. class Employee{
  71.     String name;
  72.     int salary;
  73.    
  74.     public Employee(String name, int salary){
  75.         this.name = name;
  76.         this.salary = salary;
  77.     }
  78.    
  79.     public String toString(){
  80.         return "Name: " + this.name + ", salary: " + this.salary;
  81.     }
  82.    
  83.     public String getName(){
  84.         return this.name;
  85.     }
  86.    
  87.     public void setName(String name){
  88.         this.name = name;
  89.     }
  90.    
  91.     public Object clone(){
  92.         Employee temp = new Employee(this.name, this.salary);
  93.         return temp;        
  94.     }
  95. }
  96.  
  97. /*
  98. Output of above given Java ArrayList clone example would be
  99. Original ArrayList [Name: Name Updated, salary: 10000, Name: Steve, salary: 20000, Name: Alex, salary: 30000]
  100. Cloned ArrayList [Name: Name Updated, salary: 10000, Name: Steve, salary: 20000, Name: Alex, salary: 30000]
  101. ArrayList cloned - Deep copied
  102. Original ArrayList [Name: Mark, salary: 10000, Name: Steve, salary: 20000, Name: Alex, salary: 30000]
  103. New ArrayList [Name: Name Updated, salary: 10000, Name: Steve, salary: 20000, Name: Alex, salary: 30000]
  104. */
Your rating: None Average: 4 (3 votes)
Share this

Post new comment

1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.