Even though Linked list internally use the Doubly Linked List , So it can easily identify the last element and start adding. Now still it's slow compare to array list why ?
Please refer the below code with output :
package com.collection;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Performance {
public static void main(String[] args) {
List<Integer> arrayList = new ArrayList<Integer>();
List<Integer> LinkedList = new LinkedList<Integer>();
checkPerformance("arrayList", arrayList);
checkPerformance("LinkedList", LinkedList);
}
private static void checkPerformance(String type, List<Integer> list) {
for (int i = 0; i < 1E6; i++) {
list.add(i);
}
long start = System.currentTimeMillis();
for (int i = 0; i < 1E6; i++) {
list.add(i);
}
long end = System.currentTimeMillis();
System.out.println("Time Taken" + " " + (end - start)
+ " ms for the type " + type);
}
}
Output : Time Taken 250 ms for the type arrayList Time Taken 390 ms for the type LinkedList