What is the most efficient way to remove an item from the ArrayList? [closed]

0

I would like to know which of the methods available by class ArrayList is most efficient for deleting an item from the list.

    
asked by anonymous 27.05.2017 / 23:11

1 answer

7

The two forms provided by the class ArrayList are:

meuArrayList.remove(indice);

Where indice is the position of the element in the list or:

meuArrayList.remove(elemento);

Where elemento is an exact occurrence of some element that is in the list.

  

Note : The second form will only work properly if your list is of some native java type (like String, Integer, etc ...).   If you create your own objects, this method will only work correctly   the equals() method is overwritten. Nesta   answer explains how   do it right.

According to the source code of the methods remove(index) and remove(Element) ", you can see that the first option does not scroll the entire list, removing directly in the index of the element, while the second will go through until you find the first occurrence of the last element to remove.

Follow the code to test the difference:

import java.util.ArrayList;

public class ArrayListRemocaoTest {

    public static void main(String[] args) {

        System.out.println("- Removendo o primeiro item/indice");       
        executarTeste("0");
        System.out.println();
        System.out.println("- Removendo um item/indice intermediario");
        executarTeste("5000");
        System.out.println();
        System.out.println("- Removendo o ultimo item/indice");
        executarTeste("9999");

    }

    private static void executarTeste(String valueIndex) {

        ArrayList<String> array1 = new ArrayList<>(10000);
        ArrayList<String> array2 = new ArrayList<>(10000);

        for(int i = 0; i < 10000; i++){
            array1.add(i+"");
            array2.add(i+"");
        }

        int indice = Integer.valueOf(valueIndex);

        long startTime = System.nanoTime();
        array1.remove(indice);
        long endTime = System.nanoTime();

        double duration1 = (endTime - startTime)/1000000d;

        System.out.println("Tempo de remoção do remove(index)(ms): " + String.format("%.3f", duration1));       

        startTime = System.nanoTime();
        array2.remove(valueIndex);
        endTime = System.nanoTime();

        double duration2 = (endTime - startTime)/1000000d;

        System.out.println("Tempo de remoção do remove(Element)(ms): " + String.format("%.3f", duration2));

        System.out.println("Diferença de tempo entre as duas formas: " + String.format("%.3f", (duration2-duration1)));
    }

}

In the above code there are 3 tests I did using the eclipse IDE, with two lists with a size of 10,000 items (the code can be tested on ideone ). The tests are as follows:

  • Remove the first element from the lists with both methods;
  • Remove an intermediate element (such as index 5000) from lists with both methods;
  • Remove the last element from the lists with both methods;

And the results of the implementation were:

- Removendo o primeiro item/indice
Tempo de remoção do remove(index)(ms): 0,022
Tempo de remoção do remove(Element)(ms): 0,036
Diferença de tempo entre as duas formas: 0,014

- Removendo um item/indice intermediario
Tempo de remoção do remove(index)(ms): 0,014
Tempo de remoção do remove(Element)(ms): 0,673
Diferença de tempo entre as duas formas: 0,659

- Removendo o ultimo item/indice
Tempo de remoção do remove(index)(ms): 0,035
Tempo de remoção do remove(Element)(ms): 1,339
Diferença de tempo entre as duas formas: 1,304

Notice that in the three tests, removing by index was faster, but the time difference is almost derisory. The result varies with each execution, but you can see a small difference between them.

What really impacts test values is that in both forms of removal, a copy of the list is created, where the index to be removed from the list is moved to the last position, and then applied null to this list. position, letting the Garbage Collector do the rest.

Depending on the use of hardware features at this time of copying the list, even more so if it is a large-sized list like the above test, it will affect the test time for more or less time. If you do not want this behavior, you might want to use a LinkedList .

    
27.05.2017 / 23:11