ArrayList and LinkedList differences in Java

Source: Internet
Author: User

The general differences between ArrayList and LinkedList are as follows:
1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list.
2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer.
3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.

On the code:

 1 static final int n=50000;  2 Static long Timelist (List list) {3 long Start=system.currenttimemillis (); 4 object o = new Object (); 5 for (int i=0;i<n;i++) {6 list.add (0, O); 7} 8 return System.currenttimemillis () -start; 9}10 Static long readlist (list list) {One long Start=system.currenttimemillis (); (int i=0,j=li St.size (); i<j;i++) {}15 return System.currenttimemillis ()-start;16}17 static List Addl         IST (List List) {Object o = new Object (); (int i=0;i<n;i++) {list.add (0, O); 22 }23 return list;24}25 public static void Main (string[] args) {System.out.println ("Arrayli St adding "+n+" strips time-consuming: "+timelist (New ArrayList ())); System.out.println (" LinkedList adding "+n+" strips Time: "+timelist (new LinkedList () ); list list1=addlist (New arraylist<> ()); list list2=addlist (new linkedlist&Lt;> ()); System.out.println ("ArrayList find" +n+ "Strip Time:" +readlist (List1)); System.out.println (LinkedList Finding "+n+" strips time-consuming: "+timelist (List2)); 33}

When we load 50,000 data in the collection, the test run results are as follows:

Obviously we can see that ArrayList is more suitable for reading data and LinkedList adding or deleting data more often.

ArrayList internally is implemented using an extensible array, so it takes constant time to use the get and set methods, but if you insert and delete elements, the code is expensive, unless the insertion and deletion positions are at the end of the table, because it requires the movement of the array.
LinkedList is implemented using a doubly linked list, so get is very resource intensive, unless the location is close to the head. However, inserting and deleting elements takes a constant amount of time.

ArrayList and LinkedList differences in Java

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.