Anyone noticed how LinkedList beats ArrayList in the CodeGym insertion test.
Yet the general consensus is ALWAYS use ArrayList's - even the author (Joshua Bloch) of Java’s Collections framework said
'Does anyone actually use LinkedList? I wrote it, and I never use it.'
Then I noticed
list.add(0, new Object());
Isn't the index of 0 just causing the ArrayList to have to push all the elements along by one place each time, rather than just adding it to the end?package en.codegym.task.jdk13.task08.task0809;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
/*
Measure how long it takes to perform 10,000 insertions on each list.
*/
public class Solution {
public static void main(String[] args) {
System.out.println(getInsertTimeInMs(new ArrayList()));
System.out.println(getInsertTimeInMs(new LinkedList()));
}
public static long getInsertTimeInMs(List list) {
long start = System.currentTimeMillis();
insert10000(list);
long finish = System.currentTimeMillis();
return(finish - start);
}
public static void insert10000(List list) {
for (int i = 0; i < 10000; i++) {
list.add(0, new Object());
}
}
}