A comparison of the Arrays.asList() and List.toArray() methods

Keywords: Java list array

Arrays and collections are containers for storing objects, the former being simple in nature and easy to use; the latter being safe in type, powerful in function, and necessarily related to each other. But if you don't pay attention to details, they are easy to tread.

Array to Set

Note whether views are used to directly return data from an array during the conversion process. What is a view? You can understand that a complete set is not generated behind it. You can also see that Arrays.asList() is named asList instead of toList.

In use, the add/remove/clear method of the object transformed by Arrays.asList() throws an UnsupportedOperationException exception because the object returned by Arrays.asList() is an internal class of Arrays that only implements set() Method, while neither add/remove/clear method is implemented, the add/remove/clear method of the parent AbstractList is called, and all three methods throw an UnsupportedOperationException exception.
The following arrow indicates that the internal class ArrayList in the Arrays class implements the set() method of AbstractList.

The following is the add/remove/clear method for AbstractList

Arrays have a knot that does not bend down for five buckets of rice. The message is "Use me directly or be careful of exceptions!". Failures caused by switching arrays to collections are still common and apply when returned collection objects are modified and valued without modifying size or appending.

Recommended usage

When using Array to Collection, it is recommended that you create a new collection in order to avoid the pit mentioned above.

List<Object> objectList = new java.util.ArrayList<Object>(Arrays.asList(array));

Set to Array

Collection to Array is more controllable as it moves from a relatively free collection container to a more demanding array.
The top level of toArray is defined in Collection, so almost all collections have this method, but the implementation is different.
Note the source code for the toArray() method.

List<String> list = new ArrayList<String>(3);
list.add("1");
list.add("2");
list.add("3");

// Cannot use String[] to receive results returned by parameterless methods because of missing templates. Do not use this method in practice
Object[] array1 = list.toArray();

// array2 array length is less than number of elements
String[] array2 = new String[2];
String[] stringsNew = list.toArray(array2);
System.out.println(Arrays.asList(array2)); // Output [null, null] because array2 is not used because a new array return is created in the toArray method
System.out.println(Arrays.asList(stringsNew)); // Output [1, 2, 3]


array2 = list.toArray(array2);
System.out.println(Arrays.asList(array2));// Output [1, 2, 3], where array2 points to the newly generated array within the toArray method

// array3 array length equals number of elements
String[] array3 = new String[3];
list.toArray(array3);
System.out.println(Arrays.asList(array3));// Output [1, 2, 3], sufficient length, direct copy

// array4 array length greater than number of elements
String[] array4 = new String[5];
array4[4] = "four";
array4[3] = "three";
list.toArray(array4);
System.out.println(Arrays.asList(array4));// Output [1, 2, 3, null, 4], length exceeded, where the element at the array4[list.size()] position is set to null, and the element behind it has no effect.

Recommended usage

In Code Out Efficiency: Alibaba Java Development Manual, it is recommended that the toArray method pass in a parameter of size() of the list, which runs faster and consumes less space. Even when converting to an array using the set's toArray(T[] array) method, be aware that an array of exactly the same type needs to be passed in, and that its size is list.size().

In the JDK16 source code, an example of how the official document uses toArray(T[] array) is

// Suppose x is a string list and it is converted to an array in the following way
String[] y = x.toArray(new String[0]);


Through many tests, the efficiency of the two methods is almost the same

If the efficiency is consistent, the author currently feels that the method of official documentation is more reliable, because there is no need to determine the size of the array beforehand, the uniform length is set to 0, and it is only used in the parameters to determine the generics.

Posted by cosmo7 on Sat, 25 Sep 2021 09:39:31 -0700