Add to end Performance compare: LinkList and ArrayList
/*
time for LinkedList = 501
time for ArrayList = 126
*/
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
publicclass ListDemo {
// number of objects to add to list
staticfinalint SIZE = 1000000;
staticlong timeList(List list) {
long start = System.currentTimeMillis();
Object obj = new Object();
for (int i = 0; i < SIZE; i++) {
// add object to the rear of the list
list.add(obj);
}
return System.currentTimeMillis() - start;
}
publicstaticvoid main(String args[]) {
// do timing for LinkedList
System.out.println("time for LinkedList = " + timeList(new LinkedList()));
// do timing for ArrayList
System.out.println("time for ArrayList = " + timeList(new ArrayList()));
}
}