"Simple Java" ArrayList vs LinkedList vs Vector

Source: Internet
Author: User

List overview

List, as its name suggests, is in order. When the list is discussed, it is better to compare it with set, the elements in set are unordered and unique; The following is a class hierarchy chart, from which we can get an overview of the overall architecture of the Java Collection Class ;

ArrayList vs LinkedList vs Vector

From the class hierarchy chart above, we can see that they all implement the list interface, which is very similar in use. The difference is mainly in their respective implementations, different implementations lead to different performance and different operations.

ArrayList is implemented for variable arrays, and its size increases dynamically when more elements are added to the ArrayList. Its elements can be accessed directly through the Get/set method, because ArrayList is essentially an array.

LinkedList is implemented for doubly linked lists, and the performance of adding and removing elements is better than ArrayList, but the performance of get/set elements is poor.

Vectors are similar to ArrayList, but they are synchronous.

If your program is thread-safe, ArrayList is a better choice. When more elements are added, vectors and ArrayList need more space. Each time the vector expands, the space increases by a factor of 50%, while the ArrayList increases.

Note: The default initial space size of the ArrayList is quite small, and it is a good practice to initialize a larger space through the constructor, which avoids the overhead of expansion.

ArrayList example
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 ());}
LinkedList Example
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 ());}

From the above code, we can find that their use is very similar, the real difference lies in their underlying implementation and operational complexity.

Vector

Vectors and ArrayList are almost the same, except that vectors are thread-safe, for this reason, their performance is worse than ArrayList. Typically, most programmers use ArrayList instead of vectors because they can make explicit synchronous operations on their own.

ArrayList and LinkedList Performance comparison

The Add () method in the table means add (e E), remove () method means remove (int index)

    • ArrayList to arbitrary add,remove operations, the time complexity is O (n), but at the end of the list of operations, its time complexity is O (1).
    • LinkedList to arbitrary add,remove operations, the time complexity is O (n), but at the end of the list of operations, its time complexity is O (1).

I use the following code to test their performance:

 PackageSimplejava;Importjava.util.ArrayList;Importjava.util.LinkedList; Public classQ24 { Public Static voidMain (string[] args) {ArrayList<Integer> arrayList =NewArraylist<integer>(); LinkedList<Integer> LinkedList =NewLinkedlist<integer>(); //ArrayList Add        LongStartTime =System.nanotime ();  for(inti = 0; I < 100000; i++) {arraylist.add (i); }        LongEndTime =System.nanotime (); LongDuration = EndTime-StartTime; System.out.println ("ArrayList Add:" +duration); //LinkedList AddStartTime =System.nanotime ();  for(inti = 0; I < 100000; i++) {linkedlist.add (i); } endTime=System.nanotime (); Duration= EndTime-StartTime; System.out.println ("LinkedList add:" +duration); //ArrayList GetStartTime =System.nanotime ();  for(inti = 0; I < 10000; i++) {arraylist.get (i); } endTime=System.nanotime (); Duration= EndTime-StartTime; System.out.println ("ArrayList Get:" +duration); //LinkedList GetStartTime =System.nanotime ();  for(inti = 0; I < 10000; i++) {linkedlist.get (i); } endTime=System.nanotime (); Duration= EndTime-StartTime; System.out.println ("LinkedList Get:" +duration); //ArrayList RemoveStartTime =System.nanotime ();  for(inti = 9999; I >= 0; i--) {arraylist.remove (i); } endTime=System.nanotime (); Duration= EndTime-StartTime; System.out.println ("ArrayList Remove:" +duration); //LinkedList RemoveStartTime =System.nanotime ();  for(inti = 9999; I >= 0; i--) {linkedlist.remove (i); } endTime=System.nanotime (); Duration= EndTime-StartTime; System.out.println ("LinkedList Remove:" +duration); }}

The results are printed as follows:

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

The difference between their performance is obvious. For add and remove operations, the LinkedList performance is good, but the get operation performs poorly. Based on the time complexity table and the test results above, we can tell when to use ArrayList or LinkedList. Simply put, the LinkedList is suitable for the following situations:

    • No large number of random access operations
    • There are a lot of add/remove operations

Link: http://www.programcreek.com/2013/03/arraylist-vs-linkedlist-vs-vector/

"Simple Java" ArrayList vs LinkedList vs Vector

Related Article

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.