The difference between LinkedList and ArrayList in Java

Source: Internet
Author: User

First, the two basic differences, the interview can be used to chit chat when with the interviewer AH
1, ArrayList realizes the basic dynamic array structure, and linked the structure based on the linked list. Linked list? What is a linked list? A: "The linked list is a non-sequential, non-sequential storage structure on a physical storage unit, and the logical order of the data elements is achieved through the order of the pointers in the linked list" Note: This sentence through the science and technology in China Encyclopedia scientific entry Writing and application work item audit.
2, for Get and set,arraylist performance is better than LinkedList, because linked to move the pointer, the trouble is very
3, for Add and remove,linkedlist to be better than ArrayList, because the list is good at this, ArrayList also want to move the data
LinkedList and ArrayList are two collection classes that store a series of object references (references). For example, we use ArrayList to store a series of string or integer. So what's the difference in performance between ArrayList and LinkedList?
If we have a large list of elements that are already in order, we'll search the column for binary search and compare the speed of the two lists with the following source code:

 Public classtestlist { Public StaticFinalintN =50000; Public StaticList<integer> values;Static{Integer vals[] =NewInteger[n]; Random random =NewRandom (); for(intI=0, currval=0; i<n;i++) {Vals[i] =NewInteger (Currval); Currval + = Random.nextint ( -)+1;    } values = Arrays.aslist (Vals); }Static LongTimelist (list<integer> lst) {LongStart = System.currenttimemillis (); for(intI=0; i<n;i++) {intindex = Collections.binarysearch (LST, values.Get(i));if(Index! = i) {System. out. println ("Error occoured"); }        }returnSystem.currenttimemillis ()-Start; } Public Static void Main(string[] args) {System. out. println ("The Time of ArrayList:"+timelist (NewArraylist<integer> (values)); System. out. println ("The Time of LinkedList:"+timelist (NewLinkedlist<integer> (values)); }}

ArrayList Time: 12
LinkedList Time: 5346
Obviously, the time of ArrayList is obviously less than LinkedList. The dichotomy lookup uses a random access policy, and LinkedList does not support fast random access.
Let's look at an example of a large number of additions and deletions to the list:

 Public classListdemo { Public StaticFinalintN =50000;Static LongTimelist (list<object> List) {LongStart = System.currenttimemillis (); Object o =NewObject (); for(intI=0; i<n;i++) {List.add (o); }returnSystem.currenttimemillis ()-Start; } Public Static void Main(string[] args) {System. out. println ("The Time of ArrayList:"+timelist (NewArraylist<object> ())); System. out. println ("The Time of LinkedList:"+timelist (NewLinkedlist<object> ())); }}

ArrayList time: 5
LinkedList Time: 4
The result is beyond my expectation, I set n respectively for 5000, 500000 they are very close to the time-consuming, it seems that the theoretical knowledge and the actual situation there is a certain gap. When I increased the N to 500W, the LinkedList time exceeded the ArrayList. I hope to see the truth of my article of the Great God can explain to me, thank you.

The difference between LinkedList and ArrayList 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.