Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
ArrayList vs LinkedList
For Introduction to Java Programming
By Y. Daniel Liang
Note: you can read this supplement with Chapter 20.
ArrayList and LinkedList both are List for storing a sequence of elements.
These two data structures have the same operations for processing and manipulating data.
Why do we need these two data structures with the same functionality? Can we just use
one of the two? The answer is that we need both, because they have different
performance of certain operations. LinkedList is efficient of inserting and deleting
elements at the beginning of a list and ArrayList is efficient for all other operations.
This supplement gives two examples that demonstrate the performance of these two data
structures. You will know why their performance is different when you learn how these
two data structures are implemented later in the text.
Listing 1 gives an example that inserts one million integers to the beginning of an
ArrayList and a LinkedList. From the sample output, you see that LinkedList
is much faster than an ArrayList in this example.
Listing 1 ArrayListvsLinkedList1.java
1
2
3
4
5
6
7
8
9
10
import java.util.*;
public class ArrayListvsLinkedList1 {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new LinkedList<>();
test(list1, "Time for ArrayList is ");
test(list2, "Time for LinkedList is ");
}
11
12
13
14
15
16
17
18
19
20
21
22
public static void test(List<Integer> list, String title) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
list.add(0, i);
}
long time = System.currentTimeMillis() - startTime;
System.out.println(title + time + " milliseconds");
}
}
<margin note (line 8)>using ArrayList
<margin note (line 9)>using LinkedList
<margin note (line 16)>insert an element
<margin note (line 19)>get time
<end Listing 1>
<output>
Time for ArrayList is 82919 milliseconds
Time for LinkedList is 264 milliseconds
<end output>
Listing 2 gives an example that retrieves an element from a given index in the list.
Listing 2 ArrayListvsLinkedList2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ArrayListvsLinkedList2 {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new LinkedList<>();
test(list1, "Time for ArrayList is ");
test(list2, "Time for LinkedList is ");
}
public static void test(List<Integer> list, String title) {
for (int i = 0; i < 1000000; i++) {
list.add(i);
}
19
20
21
22
23
24
25
26
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
list.get((int)(Math.random() * 1000000));
}
long time = System.currentTimeMillis() - startTime;
System.out.println(title + time + " milliseconds");
}
}
<margin note (line 8)>using ArrayList
<margin note (line 9)>using LinkedList
<margin note (line 16)>insert an element
<margin note (line 19)>get time
<end Listing 1>
<output>
Time for ArrayList is 3 milliseconds
Time for LinkedList is 8785 milliseconds
<end output>
The program creates an ArrayList (line 7) and a LinkedList (line 8) and invokes
the test method for both lists (lines 10-11). The test method creates an array of one
million elements (lines 15-17) and obtains the time for obtaining an element from the list
at a random index (line 21). As shown in the sample output, the test time for
ArrayList is much faster than for the LinkedList.
You may revise the code to test other operations. You will find that ArrayList is
better than LinkedList for all operations except inserting and deleting elements at the
beginning of the list.