ArrayList - Example of addAll(Collection c) in Java
In this tutorial, we'll see what is the use of the method addAll() of the java.util.ArrayList. This method is used to add all the elements of an object collection (which implements the interface java.util.List or java.util.Set) to ArrayList.In this example, we are adding all the elements of a LinkedList to another ArrayList by calling the method addAll().
import java.util.ArrayList;Runtime:
import java.util.LinkedList;
import java.util.List;
public class ArrayListaddAll{
public static void main(String[] args) {
// Create an ArrayList< String>
ArrayList< String> aList = new ArrayList< String> ();
//add strings to ArrayList
aList.add("aa");
aList.add("bb");
aList.add("cc");
System.out.println("ArrayList before concatenation:");
for(String e:aList)
System.out.println(e);
//LinkedList Statement
List< String> lList = new LinkedList< String> ();
//add values to LinkedList
lList.add("dd");
lList.add("ee");
lList.add("ff");
//Apple the addAll()
aList.addAll(lList) method;
System.out.println("ArrayList after concatenation:");
for(String e:aList)
System.out.println(e);
}
}
ArrayList before concatenation:
aa
bb
cc
ArrayList after concatenation:
aa
bb
cc
dd
ee
ff