Preface:
This article introduces the comparison of the advantages and disadvantages of the three situations of converting arrays into List in Java, as well as the comparison of application scenarios, and the analysis of the causes of type conversion errors that programmers often make.
1. The most common way (not necessarily the best)
After converting the array into List through Arrays.asList(strArray) , you cannot add or delete the List. You can only check and modify it, otherwise an exception will be thrown.
Key code: List list = Arrays.asList(strArray);
private void testArrayCastToListError() { String[] strArray = new String[2]; List list = Arrays.asList(strArray); //Insert a data into the converted list list.add("1"); System.out.println(list); }Execution results:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at com.darwin.junit.Calculator.testArrayCastToList(Calculator.java:19)
at com.darwin.junit.Calculator.main(Calculator.java:44)
The program throws an exception at list.add(“1”) . UnsupportedOperationException.
Reason analysis:
The return value of Arrays.asList(strArray) is a private static inner class java.util.Arrays class java.util.Arrays.ArrayList , which is not java.util.ArrayList class. java.util.Arrays.ArrayList class has set() , get() , contains() and other methods, but does not have the method of adding add() or removing remove() , so calling add() will cause an error.
Usage scenario: Arrays.asList(strArray) method can only be used after converting an array to a List. There is no need to add or delete the values in it, and it is only used as a data source to read.
2. After the array is converted to List, it supports the method of adding, deleting, modifying and searching.
Through the ArrayList constructor, convert the return value of Arrays.asList(strArray) from java.util.Arrays.ArrayList to java.util.ArrayList .
Key code: ArrayList<String> list = new ArrayList<String>(Arrays.asList(strArray)) ;
private void testArrayCastToListRight() { String[] strArray = new String[2]; ArrayList<String> list = new ArrayList<String>(Arrays.asList(strArray)) ; list.add("1"); System.out.println(list); }Execution result: An element "1" was successfully added.
[null, null, 1]
Usage scenario: After converting the array into a List, you need to add, delete, modify and check the List. It can be used if the amount of data in List is not large.
3. Through the collection tool class Collections.addAll() method (most efficient)
Convert through Collections.addAll(arrayList, strArray) , create a List of the same length according to the length of the array, and then convert the elements in the array into binary through Collections.addAll() method, and then add it to the List. This is the most efficient method.
Key Code:
ArrayList< String> arrayList = new ArrayList<String>(strArray.length);Collections.addAll(arrayList, strArray);
test:
private void testArrayCastToListEfficient(){ String[] strArray = new String[2]; ArrayList< String> arrayList = new ArrayList<String>(strArray.length); Collections.addAll(arrayList, strArray); arrayList.add("1"); System.out.println(arrayList); }Execution result: Also successfully appended an element "1".
[null, null, 1]
Usage scenario: After converting the array into a List, you need to add, delete, modify and check the List. When the List's data volume is huge, it is preferred to use it, which can improve the operation speed.
Note: Attach the source code of Collections.addAll() method:
public static <T> boolean addAll(Collection<? super T> c, T... elements) { boolean result = false; for (T element : elements) result |= c.add(element);//result and c.add(element) bitwise or operation, and then assign the value to result return result; }Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.