Adding Arrays in to ArrayList

arraylist, arrays, java

Solution

Both approaches produce the same result, so in that respect they are equivalent.

The second one, however, is wasteful. `Arrays.asList` does not allocate additional memory - it just wraps a given array in a `List`-like API. Creating a `new ArrayList`, on the other hand, allocates, albeit temporarily, another array with the same size, and copies all the values from the source array to the internal array of the `ArrayList`'s implementation.

With small arrays it's doubtful you'd even notice the difference, but the first approach is definitely more efficient.

Problem

I can addAll array elements in to `ArrayList` by following two ways, First, ``` List<String> list1 = new ArrayList<String>(); list1.addAll(Arrays.asList("23,45,56,78".split(","))); System.out.println(list1); ``` Second, ``` List<String> list2 = new ArrayList<String>(); list2.addAll(new ArrayList<String>(Arrays.asList("23,45,56,78".split(",")))); System.out.println(list2); ``` Both works fine. And my question is Is there any difference between these two. And which one can be used for better practice Why ?

Original source