Comparison of Arraylist, vector and LinkedList

Source: Internet
Author: User

??

Summarize

??

First of all, their underlying implementations are different,Arraylist and vectors are based on arrays, and LinkedList is based on linked lists.

??

In addition, vector and ArrayList are also based on the implementation of the array, but there are many synchronized methods in vector source code, the description is thread-safe, synchronous, and ArrayList is thread insecure, non-synchronous

??

Due to the addition of many synchronized methods, the vector will be slow

??

For example, I also add 2,000,000 the same string to the two data structures to compare the time of its insertion

??

Millis have passed when using Arraylist.

124 Millis have passed when using vectors.

??

Test code

??

public class Arraylistandlinkedlistandvector {

private static final String base = "base string.";

private static final int count = 2000000;

??

public static void Arraylisttest () {

Long begin, end;

Begin = System.currenttimemillis ();

ArrayList list = new arraylist<> ();

for (int i = 0; i < count; i++) {

List.add ("add");

}

End = System.currenttimemillis ();

System.out.println ((End-begin)

+ "Millis have passed when using Arraylist.");

}

??

public static void Linkedlisttest () {

Long begin, end;

Begin = System.currenttimemillis ();

LinkedList list = new linkedlist<> ();

for (int i = 0; i < count; i++) {

List.add ("add");

}

End = System.currenttimemillis ();

System.out.println ((End-begin)

+ "Millis have passed when using LinkedList.");

}

??

public static void Vectortest () {

Long begin, end;

Begin = System.currenttimemillis ();

Vector vector = new vector<> ();

StringBuilder test = new StringBuilder (base);

for (int i = 0; i < count; i++) {

Vector.add ("add");

}

End = System.currenttimemillis ();

System.out.println ((End-begin)

+ "Millis have passed when using Vector.");

}

??

public static void Main (string[] args) {

Arraylisttest ();

Linkedlisttest ();

Vectortest ();

??

}

? ?

Comparison of Arraylist, vector and LinkedList

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.