ArrayList vs. LinkedList vs. Vector

Source: Internet
Author: User

Translated from: ArrayList vs. LinkedList vs. Vector

1. Overview of the list

Just like its name, list is an ordered sequence of elements. It is a good idea to compare it to set (a collection of 22 and unordered elements) when we are discussing the list. The following is a class-level diagram of a container. The general concept of Java containers can be understood from this level diagram.

2.ArrayList vs. LinkedList vs. Vector

They all implement the List interface, and the usage is very similar. Their main difference is that their implementation results in different performance for different operations.
ArrayList is implemented as a variable-sized array. When more elements are added to the ArrayList, the size of it increases dynamically. Because ArrayList is essentially an array, its elements can be accessed directly through the get () and set () methods.
LinkedList is implemented as a doubly linked list, which performs better than ArrayList when adding and removing elements, and is less than ArrayList for the get () and set () methods.
The Vector is similar to ArrayList, but it is thread-synchronized.
ArrayList is a better choice when the program is thread-safe. When more elements are added, ArrayList and vectors request more space, and the vector requests twice as much space each time as the current size, while ArrayList increases the current size by only 50% of the space. The LinkedList also implements the Queue interface, making it more methods than ArrayList and vectors, such as offer (),Peek (),poll () and so on.
Note : The initial capacity of the ArrayList is very small, so it is a good practice to construct ArrayList with a larger initial capacity, which avoids the cost of reallocating space.

3.ArrayList Example
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
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 shown in the example above, they are very similar in usage. The real difference lies in the implementation behind them and the complexity of their operations.

5.Vector

Vectors and ArrayList are almost identical, and the difference is that vectors are thread-synchronized. Because of this, vectors have an extra overhead than ArrayList. Typically, most Java programmers use ArrayList because they can synchronize on their own explicit implementation.

Performance comparison of 6.ArrayList and LinkedList

Time complexity is compared as follows:

* table in Add () means Add (e), remove () refers to remove (int index)
*arraylist the time complexity of adding/removing operations at any location is O (n), However, the time complexity at the end of the list is O (1).
*linkedlist the time complexity of adding/removing operations at any location is O (n), but the time complexity at the head/tail of the list is O (1). The
can test their performance with the following code:

arraylist<integer> ArrayList = new arraylist<integer> (); linkedlist<integer> LinkedList = new linkedlist<integer> ();//ArrayList Addlong startTime = system.nanotime (); for (int i = 0; i < 100000; i++) {arraylist.add (i);} Long endTime = System.nanotime (); Long duration = Endtime-starttime; System.out.println ("ArrayList Add:" + duration);//LinkedList addstarttime = System.nanotime (); for (int i = 0; I < 100000; i++) {linkedlist.add (i);} EndTime = System.nanotime ();d uration = Endtime-starttime; System.out.println ("LinkedList Add:" + duration);//ArrayList GetStartTime = System.nanotime (); for (int i = 0; I < 10000; i++) {arraylist.get (i);} EndTime = System.nanotime ();d uration = Endtime-starttime; System.out.println ("ArrayList Get:" + duration);//LinkedList getstarttime = System.nanotime (); for (int i = 0; I < 10000; i++) {linkedlist.get (i);} EndTime = System.nanotime ();d uration = Endtime-starttime; System.out.println ("LinkedList Get:"+ duration);//ArrayList Removestarttime = System.nanotime (); for (int i = 9999; I >=0; i--) {arraylist.remove (i);} EndTime = System.nanotime ();d uration = Endtime-starttime; System.out.println ("ArrayList Remove:" + duration);//LinkedList removestarttime = System.nanotime (); for (int i = 9999; I >=0; i--) {linkedlist.remove (i);} EndTime = System.nanotime ();d uration = Endtime-starttime; System.out.println ("LinkedList Remove:" + duration);

The output is as follows:

ArrayList add:  13265642LinkedList add:9550057arraylist get:  1543352LinkedList get:85085551arraylist Remove:  199961301LinkedList remove:85768810


The performance difference is obvious, LinkedList is faster in adding/removing elements, and slower when accessing elements. Based on the time complexity table and the test results, we can know when to use ArrayList or LinkedList. To put it simply, LinkedList should be used in the following situations:
There is no large number of random access elements to the operation;
There are lots of actions to add/remove elements.

Translated from: ArrayList vs. LinkedList vs. Vector

ArrayList vs. LinkedList vs. Vector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.