Conversion difference from Array to ArrayList with "asList" and with constructor

2

What's the difference between these two ways of converting an array ? If there is any difference, does it impact performance?

List<String> list = Arrays.asList(meuArray);

ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(meuArray));
    
asked by anonymous 05.11.2018 / 13:52

1 answer

3

The first one is assigning a list to list , but this list will be based on the array being used as the base ( meuArray ), so the list is only one reference to the list.

The second does the same with the constructor argument, but the constructor of ArayList has the function of copying the content it receives as a list, so it will take the received reference and copy each element to a new list, so it is very but it will have an isolated list.

The question is whether to keep the same list or not. The difference is in the constructor and not in the asList() method. The first does not build anything, it just uses what has already been built by asList() , and this method usually does not build anything strong either, it even builds a list object, but it will reference the array of direct form and will not make any copies.

    
05.11.2018 / 14:17