ArrayList and LinkedList performance comparisons of Java collection classes

Source: Internet
Author: User

With regard to the performance of the two collection classes, ArrayList and LinkedList, many articles on the web indicate that the insertion performance of ArrayList is worse than LinkedList. Today, I suddenly want to test, whether this conclusion is accurate.

The following code is written:

Import java.util.arraylist;import java.util.linkedlist;import java.util.list;public class  demo {public static void main (String[] args)  {int count =  1000000;//Cycles System.out.println ("loops  "  + count +  "  times, Arraylist linkedlist   Rear insertion performance test: "); List<integer> lst = new arraylist<integer> (); List<integer> link = new linkedlist<integer> (); New Demo (). TestInsert (LST,  count); New demo (). Testinsert (link, count); int index = 0;//Insert Position count =  90000; System.out.println ("\ n loop  "  + count +  "  secondary,arraylist linkedlist  Specify location Insert performance test: ");lst = new arraylist<integer> ();link = new linkedlist< Integer> (); New demo (). Testinsertforindex (Lst, count, index); New demo (). Testinsertforindex (Link, count, index);}/** *  Insert elements  *  @param  count cycles to default location  */public void testinsert (list< Integer> lst, int count) {long btime = system.currenttimemillis (); for (int  i=0; i<count; i++) {lst.add (1);} Long etime = system.currenttimemillis (); System.out.println (Lst.getclass (). GetName ()  +    Total time:  +  (Etime - btime)  +  " ms");} /** *  inserts elements  *  @param  count cycles  *  @param  index insertion position to the specified location  */public  void testinsertforindex (List<integer> lst, int count, int index) { Long btime = system.currenttimemillis (); for (int i=0; i<count; i++) {Lst.add ( index,1);} Long etime = system.currenttimemillis (); System.out.println (Lst.getclass (). GetName ()  +    Total time:  +  (Etime - btime)  +  " ms");}}

The results of the implementation are as follows:

Loop 1 million times, ArrayList linkedlist tail Insert performance test: Java.util.ArrayList Total time: msjava.util.LinkedList Total time: 192 ms Cycle 90,000 times, ArrayList LinkedList Specify location insertion performance test: Java.util.ArrayList total time: 3885 msjava.util.LinkedList Total time: 5 ms


According to the results, when adding elements using the Add (e) function, LinkedList's performance is not as good as ArrayList. So, what the hell is this?

Let's look at the specific source code:

The Add (e-e) function of the ArrayList

Public boolean Add (E e) {ensurecapacity (size + 1);    Array expansion elementdata[size++] = e; return true;}

The first line of code: used to expand the array of elements in the ArrayList, and first determine if the need for expansion, if necessary, the expansion algorithm is: the current capacity *3/2+1

Second line of code: Append new elements to the end of an array of elements


The Add (e-e) function of the LinkedList

Public boolean Add (E e) {Addbefore (E, header); return true;} Private entry<e> Addbefore (e E, entry<e> Entry) {entry<e> newEntry = new Entry<e> (e, Entry, en    try.previous);    NewEntry.previous.next = NewEntry;    newEntry.next.previous = NewEntry;    size++;    modcount++; return newEntry;}

The Addbefore function first creates a new entry node, inserts it into the linked table, and then adjusts the backward reference of the previous node of the new node and the forward reference of the next node, respectively.


See the two pieces of code before the conclusion also has the answer.


Let's see why the performance gap is so great when inserting into a given location.

ArrayList Add (int index, E Element) function

public void Add (int index, E element) {if (Index > Size | | Index < 0) throw new indexoutofboundsexception    ("Index:" +index+ ", Size:" +size);  Ensurecapacity (size+1);    Increments modcount!!    System.arraycopy (Elementdata, index, elementdata, index + 1,size-index);    Elementdata[index] = element; size++;}

As you can see here, each insertion of an element uses System.arraycopy to replicate the array, and it is conceivable how low the efficiency will be.


LinkedList Add (int index, E Element) function

public void Add (int index, E element) {Addbefore (element, (index==size. Header:entry (index)));}

Here, the Addbefore function is still called, with no difference between performance and add (e e).


Summary: As a coder, every detail should be verified by themselves, can not conform.

This article is from the "Life in full" blog, be sure to keep this source http://shane.blog.51cto.com/824878/1828196

ArrayList and LinkedList performance comparisons of Java collection classes

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.