??
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