The difference between ArrayList and LinkedList __java

Source: Internet
Author: User
Generally we all know the general difference between ArrayList and LinkedList:
1.ArrayList is a data structure based on dynamic array, LinkedList based on the data structure of the 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 more advantageous for new and delete operations because ArrayList want to move the data.
This depends on the actual situation. If only the single data inserted or deleted, ArrayList speed is better than LinkedList. But if the batch random inserts deletes the data, the LinkedList speed is much better than ArrayList.  Because ArrayList each insertion of data, you move the insertion point and all subsequent data. This I did the experiment. In the first 200,000 "records" of ArrayList and LinkedList insert 20,000 data, LinkedList time is about ArrayList 20 1. for (int m=0;m<20000;m++) {
Linkedlist.add (M, null); When inserting 20,000 data before 200,000 data, LinkedList only uses more than 1125 Ms. That's where LinkedList's strength lies.
}
Long time4 = new Dte (). GetTime ();
System.out.print ("Batch LinkedList add:");
System.out.println (Time4-time3);
for (int n=0;n<20000;n++) {
Arraylist.add (n, NULL); When inserting 20,000 data before 200,000 data, ArrayList used more than 18,375 Ms. The time spent is nearly 20 times times ArrayList (depending on the machine performance when testing)
}
Long time5 = new Date (). GetTime ();
System.out.print ("Batch ArrayList add:");
System.out.println (TIME5-TIME4);


4. Find Operation Indexof,lastindexof,contains and so on, the two are similar.
5. Random lookup of the specified node operation get,arraylist faster than LinkedList.
This is just a theoretical analysis, in fact, not necessarily, ArrayList at the end of inserting and deleting data, but faster than LinkedList.          I did a test that inserts and deletes 200,000 data. Long time1 = new Date (). GetTime ();
string S1 = (string) linkedlist.get (100000); The total record 200000,linkedlist loading 100,000th data is time-consuming 15~32ms unequal
Long time2 = new Date (). GetTime ();
System.out.println (TIME2-TIME1);
String s2 = (string) arraylist.get (100000); Total record 200000,linkedlist loading 100,000th data 0ms time consuming
Long Time3 = new Date (). GetTime ();
System.out.println (time3-time2);

/* Separate insert200000 data to LinkedList and ArrayList
* As the data is inserted at the end, the speed of ArrayList is faster than that of LinkedList
*/
public static void Insertlist (LinkedList linklist, ArrayList ArrayList) {
Long time1 = new Date (). GetTime ();
System.out.println (TIME1);
for (int i = 0; i < 200000; i++) {
Linklist.add (i, "linklist" + i);
}
Long time2 = new Date (). GetTime ();
System.out.println (TIME2-TIME1);
for (int j = 0; J < 200000; J + +) {
Arraylist.add (J, "ArrayList" + j);
}
Long Time3 = new Date (). GetTime ();
System.out.println (time3-time2);
}


/* Delete 200,000 data in LinkedList and ArrayList
* Due to the deletion of data at the end, ArrayList faster than LinkedList speed
*/
public static void DeleteList (LinkedList linklist, ArrayList ArrayList) {
Long time1 = new Date (). GetTime ();
System.out.println (TIME1);
for (int i = 199999 i >= 0; i--) {
Linklist.remove (i);
}
Long time2 = new Date (). GetTime ();
System.out.println (TIME2-TIME1);
for (int j = 199999 J >= 0; j--) {
Arraylist.remove (j);
}
Long Time3 = new Date (). GetTime ();
System.out.println (time3-time2);
}

public static void Main (String args[]) {
LinkedList LinkedList = new LinkedList ();
ArrayList ArrayList = new ArrayList ();
Insertlist (LinkedList, ArrayList);

The following code omits

Insert:
LinkedList 578ms
ArrayList 437ms
Delete:
LinkedList 31ms
ArrayList 16ms

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.