I made an ArrayList and a LinkedList, filled them with random numbers (both collections have the same data). Later I tried to measure time consumption of certain operations and found out that linked list outperformed ArrayList in everyone of them. Especially I am concerned about the search test because it looks very counterintuitive for me. It's really weird. ` The part where I invoke "indexOf"
ps.println("\nSearch Test");
startTime = System.nanoTime();
ps.println(arrayList.indexOf(12));
estimatedTime = System.nanoTime() - startTime;
ps.println("ArrayList: " + estimatedTime);
startTime = System.nanoTime();
ps.println(linkedList.indexOf(12));
estimatedTime = System.nanoTime() - startTime;
ps.println("LinkedList: " + estimatedTime);
`
ArrayList<Integer> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
for (int i = 0; i < 10000000; i++){
Integer num = rand.nextInt() % 1000;
arrayList.add(num);
linkedList.add(num);
}
`
The last Result for ArrayList: 397000 The last Result for LinkedList: 115541
I tried restarting the program multiple times, same result every time. It's weird. There must be an issue in my code.