[Java] Three methods and comparison of converting array to List

This article introduces the comparison of the pros and cons of converting an array to a List in Java, the comparison of application scenarios, and the analysis of the causes of type conversion errors that programmers often make.

1. (Most efficient) Through the Collections.addAll() method of the collection tool class, methods such as adding, deleting, modifying and checking are supported

Through Collections.addAll(arrayList, strArray)method conversion, create a List with the same length according to the length of the array, and then Collections.addAll()add the elements in the array to the List through the method, which is the most efficient method.

Usage scenario : After the array is converted into a List, it is necessary to add, delete, modify and query the List. It is preferred to use it when the amount of data in the List is huge, which can improve the operation speed.

Note: Attach Collections.addAll()the source code of the method:

		@SafeVarargs
    public static <T> boolean addAll(Collection<? super T> c, T... elements) {
    
    
        boolean result = false;
        for (T element : elements)
            result |= c.add(element);
        return result;
    }

key code:

List<String> aryList = new ArrayList<>(strAry.length);
Collections.addAll(aryList, strAry);

Test code:

		@Test
    public void aryToListByCollectionsTest() {
    
    
        String[] strAry = new String[]{
    
    "张三", "李四", "王五"};
        List<String> aryList = new ArrayList<>(strAry.length);
        Collections.addAll(aryList, strAry);
        System.out.println(aryList);
        System.out.println("------ 修改 List -----");
        aryList.add("赵六");
        System.out.println(aryList);
    }

As a result of the execution, operations such as addition, deletion, modification and query can be performed on the List:

insert image description here

2. The most common method does not support addition and deletion methods (not necessarily the best)

Through Arrays.asList(strAry)the method, after the array is converted to a List, the List cannot be added or deleted, but can only be checked and modified, otherwise an exception will be thrown .

key code :List<String> aryList = Arrays.asList(strAry);

Test code:

		@Test
    public void aryToListByAsListTest() {
    
    
        String[] strAry = new String[]{
    
    "张三", "李四", "王五"};
        List<String> aryList = Arrays.asList(strAry);
        System.out.println(aryList);
        System.out.println("------ 修改 List -----");
        aryList.add("赵六");
        System.out.println(aryList);
    }

As a result of the execution, the program is executing to aryList.add("Zhao Liu"); an exception UnsupportedOperationException is thrown:

insert image description here

Reason Analysis : Arrays.asList(strArray)The return value is java.util.Arraysa private static inner class in the class java.util.Arrays.ArrayList, which is not java.util.ArrayLista class. java.util.Arrays.ArrayListThe class has set(), get(), contains() and other methods, but does not have add add()or delete remove()methods, so calling add()the method will report an error.

Usage scenario : Arrays.asList(strAry)The method can only be used after the array is converted to a List, and there is no need to add or delete values ​​in it, and it can only be used as a data source for reading.

3. After the array is converted to a List, the method of adding, deleting, modifying and checking is supported

Through the constructor of ArrayList, Arrays.asList(strArray)the return value of is java.util.Arrays.ArrayListconverted to java.util.ArrayList.

key code :ArrayList list = new ArrayList(Arrays.asList(strAry)) ;

Test code:

		@Test
    public void aryToListByArrayListTest() {
    
    
        String[] strAry = new String[]{
    
    "张三", "李四", "王五"};
        List<String> aryList = new ArrayList<>(Arrays.asList(strAry));
        System.out.println(aryList);
        System.out.println("------ 修改 List -----");
        aryList.add("赵六");
        System.out.println(aryList);
    }

As a result of the execution, operations such as addition, deletion, modification and query can be performed on the List:

insert image description here

Usage scenario : After the array is converted into a List, it is necessary to add, delete, modify and query the List. It can be used when the amount of data in the List is not large.

Guess you like

Origin blog.csdn.net/ToBeMaybe_/article/details/119647011