import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class CompareList {
public static void main(String[] args) {
List<Integer> ArrayList = new ArrayList<Integer>();
List<Integer> LinkedList = new LinkedList<Integer>();
doCalculations("ArrayList",ArrayList);
doCalculations("LinkedList",LinkedList);
}
private static void doCalculations(String type,List<Integer> List){
for(int i=0;i<1E5;i++){
List.add(i);
}
Long start = System.currentTimeMillis();
/*
To add elements in the end
for(int i=0;i<1E5;i++){
List.add(i);
}
*/
for(int i=0;i<1E5;i++){
List.add(0,i);
}
Long end = System.currentTimeMillis();
System.out.println("time taken" +" "+ (end-start) + "ms for" +" "+ type);
}
}
time taken 13ms for ArrayList time taken 64ms for LinkedList
i know this is duplicate question ,but please don't remove this ,whatever answers they gave to this question i was unable understand ,Can anyone explain this in simple words why this linked list is slower when we add element in the end?