Collections.addAll
An array has many elements. These can be added to an ArrayList
. With Collections.addAll
we can add an array of elements to an ArrayList
.
With addAll
, we must have element types that match. An Integer ArrayList
is incompatible with an int
array. Often we must use a for
-loop to add an array.
This program uses a String
array and an ArrayList
of Strings. It combines a one-element ArrayList
with a String
array of 3 elements.
ArrayList
) we are adding to, and the values we are adding.import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { String[] values = { "cat", "dog", "bird" }; // Create a one-element ArrayList. ArrayList<String> list = new ArrayList<>(); list.add("elephant"); System.out.println(list); // Add all elements to the ArrayList from an array. Collections.addAll(list, values); // Display our result. System.out.println(list); } }[elephant] [elephant, cat, dog, bird]
int
conversionHere we add an int
array to an ArrayList
with Integer elements. We use a for
-loop, as the array cannot be directly added.
ArrayList
. We do not need an index value in this loop.import java.util.ArrayList; public class Program { public static void main(String[] args) { // An int array. int[] sizes = { 100, 200, 300 }; // Create an Integer ArrayList. ArrayList<Integer> list = new ArrayList<>(); list.add(500); System.out.println(list); // Loop over our array and add each element separately. for (int element : sizes) { list.add(element); } // The final list. System.out.println(list); } }[500] [500, 100, 200, 300]
Collections.addAll
errorAn int
array cannot added to an ArrayList
of Integers. An int
is not the same thing as an Integer—an Integer is an object that contains an int
.
import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { // Has int elements. int[] sizes = { 100, 200, 300 }; // This has Integer elements. ArrayList<Integer> list = new ArrayList<>(); // Cannot add an int array to Integer ArrayList. Collections.addAll(list, sizes); } }Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method addAll(Collection<? super T>, T...) in the type Collections is not applicable for the arguments (ArrayList<Integer>, int[]) at Program.main(Program.java:14)
A cast does not help with the problems of Collections.addAll
. We cannot cast an array of ints to Integers. Here is the compilation problem.
import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { int[] sizes = { 100, 200, 300 }; ArrayList<Integer> list = new ArrayList<>(); // This cast does not work. Collections.addAll(list, (Integer[]) sizes); } }Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot cast from int[] to Integer[] at Program.main(Program.java:14)
Does the addAll
method have a performance advantage over a for
-loop that calls add? I tested this in a benchmark. I measured both constructs.
Collections.addAll
to add an array to an ArrayList
.for
-loop and the add()
method on the ArrayList
. It has 2 nested loops.addAll
on a short
String
array was consistently faster.import java.util.Collections; import java.util.ArrayList; public class Program { public static void main(String[] args) { String[] elements = { "cat", "dog", "fish" }; long t1 = System.currentTimeMillis(); // Version 1: use addAll method. for (int i = 0; i < 10000000; i++) { ArrayList<String> list = new ArrayList<>(); Collections.addAll(list, elements); if (list.size() != 3) { return; } } long t2 = System.currentTimeMillis(); // Version 2: use for-loop and add. for (int i = 0; i < 10000000; i++) { ArrayList<String> list = new ArrayList<>(); for (String element : elements) { list.add(element); } if (list.size() != 3) { return; } } long t3 = System.currentTimeMillis(); // ... Result. System.out.println(t2 - t1); System.out.println(t3 - t2); } }423 ms, Collections.addAll 463 ms, ArrayList add
There are benefits to Collections.addAll
: it reduces lines of code and avoids loops. It is probably faster than a for
-loop.
However, incompatible arrays (where int
does not match Integer) are not supported. Often to add arrays to ArrayLists
, a for
-loop is needed.