Java Collections Framework:ArrayList-6.1
How to clone an ArrayList to another ArrayList
In this tutorial we will learn how to clone an ArrayList
to another one. We would be using clone() method of ArrayList class to serve our purpose.
Object clone()
This method returns a shallow copy of the ArrayList instance.
Complete example of ArrayList Cloning
In this example we have an ArrayList of String type and we are cloning it to another ArrayList using clone() method. The interesting point to see here is when we added and removed few elements from original ArrayList after the clone() method, the cloned ArrayList didn’t get affected. It shows that clone() method just returns a shallow copy of ArrayList.
package beginnersbook.com; import java.util.ArrayList; public class Details { public static void main(String a[]){ ArrayList<String> al = new ArrayList<String>(); //Adding elements to the ArrayList al.add("Apple"); al.add("Orange"); al.add("Mango"); al.add("Grapes"); System.out.println("ArrayList: "+al); ArrayList<String> al2 = (ArrayList<String>)al.clone(); System.out.println("Shallow copy of ArrayList: "+ al2); //add and remove on original ArrayList al.add("Fig"); al.remove("Orange"); //Display of both ArrayLists after add & remove System.out.println("Original ArrayList:"+al); System.out.println("Cloned ArrayList:"+al2); } }
Output:
ArrayList: [Apple, Orange, Mango, Grapes] Shallow copy of ArrayList: [Apple, Orange, Mango, Grapes] Original ArrayList:[Apple, Mango, Grapes, Fig] Cloned ArrayList:[Apple, Orange, Mango, Grapes]
How to empty an ArrayList in Java
There are two ways to empty an ArrayList – By using ArrayList.clear() method or with the help of ArrayList.removeAll() method. Although both methods do the same task the way they empty the List is quite different.
Lets see the below example first then we will see the implementation and difference between clear() and removeAll().
package beginnersbook.com;
import java.util.ArrayList;
import java.io.*;
public class Details
{
public static void main(String [] args)
{
ArrayList<String> al1=new ArrayList<String>();
al1.add("abc");
al1.add("xyz");
System.out.println("ArrayList before clear: "+al1);
al1.clear();
System.out.println("ArrayList after clear: "+al1);
ArrayList<String> al2=new ArrayList<String>();
al2.add("text 1");
al2.add("text 2");
System.out.println("ArrayList before removeAll: "+al2);
al2.removeAll(al2);
System.out.println("ArrayList before removeAll: "+al2);
}
}
Output:
ArrayList before clear: [abc, xyz]
ArrayList after clear: []
ArrayList before removeAll: [text 1="text" 2="2" language="1,"][/text]
ArrayList before removeAll: []
As you can both the methods did the same job, they emptied the ArrayList. It’s time to determine which method gives good performance.
The actual code of clear() method:
public void clear() {
for (int i = 0; i < size; i++)
arraylist[i] = null;
size = 0;
}
Here arraylist is an instance of ArrayList class.
Code of removeAll() method:
public boolean removeAll(Collection c) {
boolean ismodified = false;
Iterator iterator = iterator();
while (iterator.hasNext()) {
if (c.contains(iterator.next())) {
iterator.remove();
ismodified = true;
}
}
return ismodified;
}
By seeing the code of both the methods we can very well say that clear() method gives better performance compared to the removeAll() method.
Performance of clear: O(n)
Performance of removeAll: O(n^2)
Java ArrayList isEmpty() Method example
isEmpty()
method ofjava.util.ArrayList
class is used for checking whether the list is empty or not. This method returns a boolean value.public boolean isEmpty()
It returns true if the list is empty otherwise it gives false.
Example
Output: