Translators: Anchor
Translation time: December 2, 2013
Original link: ArrayList vs. LinkedList vs. Vector
1. List overview
List, as shown in the name, is an ordered list of elements. When we talk about lists, it's a good idea to compare them to set, and the elements in the set set are unordered and unique.
is the collection class succession diagram, you can have a general understanding of the knowledge discussed in this article.
Figure 1
2. Comparison of ArrayList, LinkedList and vectors
As you can see, all three have implemented the List interface. All use is similar, the main difference is because the implementation of different ways, so the different operations have different efficiency.
ArrayList is a resizable array. When more elements are added to the ArrayList, their size will grow dynamically. Internal elements can be accessed directly through get and set methods, because ArrayList is essentially an array.
LinkedList is a doubly linked list that has better performance than ArrayList when adding and deleting elements. However, the get and set aspects are weaker than ArrayList.
Of course, these comparisons refer to the comparison of large data volumes or very frequent operations, and if the data and calculations are small, then the contrast will lose meaning.
vectors are similar to ArrayList, but they belong to the strong synchronization class. If your program itself is thread-safe (thread-safe, not sharing the same collection/object across multiple threads), then using ArrayList is a better choice.
Vectors and ArrayList request greater space when more elements are added. Each time the vector requests a double space for its size, ArrayList increases by 50% for size each time.
The LinkedList also implements the Queue interface, which provides more methods than list, including offer (), Peek (), poll (), and so on.
Note: The initial capacity of ArrayList is very small by default, so if you can estimate the amount of data, assigning a large initial value is a best practice, which reduces the cost of resizing.
3. ArrayList example
[Java]View Plaincopy
- Public static void Testarraylist () {
- Arraylist<integer> al = new arraylist<integer> ();
- Al.add (3);
- Al.add (2);
- Al.add (1);
- Al.add (4);
- Al.add (5);
- Al.add (6);
- Al.add (6);
- iterator<integer> iter1 = Al.iterator ();
- While (Iter1.hasnext ()) {
- System.out.println (Iter1.next ());
- }
- }
4. LinkedList Example
[Java]View Plaincopy
- Public static void Testlinkedlist () {
- linkedlist<integer> ll = new linkedlist<integer> ();
- Ll.add (3);
- Ll.add (2);
- Ll.add (1);
- Ll.add (4);
- Ll.add (5);
- Ll.add (6);
- Ll.add (6);
- Iterator<integer> iter2 = Ll.iterator ();
- While (Iter2.hasnext ()) {
- System.out.println (Iter2.next ());
- }
- }
As the example above shows, its usage is similar, the actual difference is that the underlying implementation and the complexity of the operation are different.
5. Vector
Vectors and ArrayList are almost identical, the only difference being that the vector is a synchronous class (synchronized). Therefore, the cost is greater than the ArrayList. Under normal circumstances, Most Java programmers use ArrayList instead of vectors, because synchronization can be controlled entirely by the programmer itself.
6. ArrayList and LinkedList performance comparison
The complexity of time is compared as follows:
| |
ArrayList |
linkedlist |
| Get () |
O (1) |
O (N) |
| Add () |
O (1) |
O (1) amortized |
| Remove () |
O (N) |
O (N) |
* Add () in the table represents add (e e), and remove () represents remove (int index) '
- ArrayList for random position add/remove, the time complexity is O (n), but for Add/delete operations at the end of the list, the time complexity is O (1).
- LinkedList for random position add/remove, the time complexity is O (n), but for Add/delete operations at the end/beginning of the list, the time complexity is O (1).
I use the following code to test their performance:
[Java]View Plaincopy
- Public static void Testperformance () {
- arraylist<integer> ArrayList = new arraylist<integer> ();
- linkedlist<integer> LinkedList = new linkedlist<integer> ();
- int
- Times = Ten * 1000;
- //times = 100 * 1000;
- //times = 1000 * 1000;
- System.out.println ("Test times =" + times);
- System.out.println ("-------------------------");
- //ArrayList Add
- Long startTime = System.nanotime ();
- For (int i = 0; i < times; i++) {
- Arraylist.add (i);
- }
- Long endTime = System.nanotime ();
- long duration = Endtime-starttime;
- System.out.println (Duration + "<--arraylist add");
- //LinkedList add
- StartTime = System.nanotime ();
- For (int i = 0; i < times; i++) {
- Linkedlist.add (i);
- }
- EndTime = System.nanotime ();
- Duration = Endtime-starttime;
- System.out.println (Duration + "<--linkedlist add");
- System.out.println ("-------------------------");
- //ArrayList get
- StartTime = System.nanotime ();
- For (int i = 0; i < times; i++) {
- Arraylist.get (i);
- }
- EndTime = System.nanotime ();
- Duration = Endtime-starttime;
- System.out.println (Duration + "<--arraylist get");
- //LinkedList get
- StartTime = System.nanotime ();
- For (int i = 0; i < times; i++) {
- Linkedlist.get (i);
- }
- EndTime = System.nanotime ();
- Duration = Endtime-starttime;
- System.out.println (Duration + "<--linkedlist get");
- System.out.println ("-------------------------");
- //ArrayList Remove
- StartTime = System.nanotime ();
- For (int i = times- 1; I >= 0; i--) {
- Arraylist.remove (i);
- }
- EndTime = System.nanotime ();
- Duration = Endtime-starttime;
- System.out.println (Duration + "<--arraylist remove");
- //LinkedList Remove
- StartTime = System.nanotime ();
- For (int i = times- 1; I >= 0; i--) {
- Linkedlist.remove (i);
- }
- EndTime = System.nanotime ();
- Duration = Endtime-starttime;
- System.out.println (Duration + "<--linkedlist remove");
- }
The output results are as follows:
[Plain]View Plaincopy
- Test times = 10000
- -------------------------
- 1469985 <--arraylist Add
- 3530491 <--linkedlist Add
- -------------------------
- 593678 <--arraylist Get
- 86914251 <--linkedlist Get
- -------------------------
- 625651 <--arraylist Remove
- 2164320 <--linkedlist Remove
[Java]View Plaincopy
- Test times = 100000
- -------------------------
- 11480805 <--arraylist Add
- 26384338 <--linkedlist Add
- -------------------------
- 714072 <--arraylist Get
- 10040809061 <--linkedlist Get
- -------------------------
- 1203935 <--arraylist Remove
- 1595905 <--linkedlist Remove
[Plain]View Plaincopy
- In the 1000*1000 run, a long time later, the LinkedList get log has not been printed out, about 15 minutes or so, the result is still not out.
[Java]View Plaincopy
- test times = 1000000
- -------------------------
- 132632998 <--arraylist add
- 322885939 <--linkedlist add
- -------------------------
- 3690752&NBSP;<--ARRAYLIST&NBSP;GET&NBSP;&NBSP;
- 1520315361147&NBSP;<--LINKEDLIST&NBSP;GET&NBSP;&NBSP;
- -------------------------
- 8750043&NBSP;<--ARRAYLIST&NBSP;REMOVE&NBSP;&NBSP;
- 13872885&NBSP;<--LINKEDLIST&NBSP;REMOVE&NBSP;&NBSP;
The difference in performance is quite noticeable,LinkedList is faster on add and remove, and slower on get ( This is the original).
Translator Note : The Translator's compilation and execution environment is myeclipse JDK6, regardless of how to see, are ArrayList better, so, how to choose, please according to their actual situation to decide, it is best to do their own test, because the data type is different, Different versions of JDK, different optimizations, may have different results.
Based on the time complexity table and the test results, we can determine when to use ArrayList and when to use LinkedList.
In simple terms, LinkedList is more suitable for:
- No random reads of large scale
- Large number of Add/delete operations
Compare ArrayList, LinkedList, vectors