Why does Arrays.asList() return its own ArrayList implementation
Solution 1
You asked:
Also what do you gain with the java.util.Arrays.ArrayList implementation ?
It is because the Arrays$ArrayList returned by Arrays.asList is just a view on the original array. So when the original array is changed then the view is changed too.
If one would use an real ArrayList then the elements will be copied, and a change on the orignal array would not infuence the ArrayList.
The reasons to do this are quite simple:
- performance: no need to copy anyting
- memory efficent: no second array is needed
Solution 2
The javadoc says that asList
returns "a fixed-size list backed by the specified array". If you want to resize the array, you have to create a new one and copy the old data. Than the list won't be backed by the same array instance. The stated goal is "This method acts as bridge between array-based and collection-based APIs." and so write-through to the underlying array is a design requirement.
Solution 3
They are two different classes with different behaviours.
The list returned when you called Arrays.asList
is a thin wrapper over the array, not a copy. The list returned is fixed size: attempting to call add
will throw an UnsupportedOperationException
exception.
The java.util.ArrayList
on the other hand keeps its own internal copy of the data and is variable sized.
Solution 4
Arrays.asList needs to return a list that cannot be resized -- because the underlying array cannot be resized -- but that is modifiable -- because assignment to elements in the underlying array is allowed.
Solution 5
actually you are able to add elements to the ArrayList with add. method like this :
List<String> l2= new ArrayList<String>(Arrays.asList(array1));
l2.add("blueCheese");
In my opinion you use it to get the features of a List but applying them to an Array .
Related videos on Youtube
Simeon
Seasoned developer lead that loves distributed systems, Linux, static typed languages and drawing squares connected by arrows on a board. Generally biased towards the Java ecosystem, Spring and AWS.
Updated on June 21, 2020Comments
-
Simeon almost 4 years
I recently found out that there are actually 2 different
ArrayList
implementations in Java (better late than never I guess...).So I was wondering why does
Arrays.asList(T... a)
need to return a list which can not be resized ? If they needed an unmodifiable list why add theset(int index, E element)
method then ?So my general question is why not return the
java.util.ArrayList
from theArrays.asList(T... a)
method ?Also what do you gain with the
java.util.Arrays.ArrayList
implementation ? -
jbwharris over 13 yearsTrue except for the reasons. The docs meantion only data write-through for being a bridge between array and collection bases APIs
-
Ralph over 13 years@sblundy and the docs meantion not only the bride "This method also provides a convenient way to create a fixed-size ist initialized to contain several elements". But I give you a vote up.
-
jbwharris over 13 yearswhich is the mean reason I use it.
-
David Groomes almost 9 yearsNo you are not able to add elements to the java.utils.Arrays.ArrayList returned from Arrays.asList(T ... a). Your code snippet constructs a new java.util.ArrayList, which, like you show, supports #add.
-
zwh about 5 yearsI like the "view" explanation.
-
Ralph almost 4 years@Yossarian42: no it behaves like a view: see my little test:
@Test public void testView() { Integer[] original = new Integer[] {1}; List<Integer> view = Arrays.asList(original); original[0] = 2; assertThat(view).containsExactly(2);}
-
Yossarian42 almost 4 years@Ralph Thanks. I got it wrong early.
view.set(0, 42)
will also changeoriginal[0]
. The Arrays.ArraysList contains aprivate final E[] a
reference to the original array