The main is to review the previously learned knowledge, may also be able to help the new learning Java-^
So I've opened my own blog, too.
Seriously, Java list on the web blog is also a lot, but I recently read a "data structure and algorithmic analysis of the Java language description", which is the list implementation class explanation is not bad, but also let me more they have a better understanding, so I think I will start from here, Also the knowledge of their own this aspect to consolidate again ^ ^.
my previous understanding: ArrayList: Provides an implementation of the list growth array, which takes a constant time (i.e. better performance) for a random get,set call, but it often takes a logarithmic time (poor performance) for add,remove that are not terminal. Vector: Same as ArrayList, but it is thread-safe, so sacrificing part of performance is slightly worse than ArrayList LinkedList: Provides a list of double linked table implementations, for random add, Remove tends to be less expensive, but not easy to index, so random get,set often performance worse
actual environment operation: Environment: Jdk1.8,eclipse, the following tests have a certain one-sidedness, but I think the general can reflect some things, if there is a mistake, I hope to teach ^ ^ 1. Tail Add operation:
public static void Main (string[] args) {
arraylist<integer> list1 = new arraylist<> ();
linkedlist<integer> list2 = new linkedlist<> ();
vector<integer> list3 = new vector<> ();
New Thread (
new Runnable () {public
void run () {
makeList1 (List1, 30000);
}
}). Start ();
New Thread (
new Runnable () {public
void run () {
makeList1 (list2, 30000);
}
}). Start ();
New Thread (
new Runnable () {public
void run () {
makeList1 (list3, 30000);
}
}). Start ();
}
public static list<integer> MakeList1 (list<integer> oo, int n) {
oo.clear ();
Long begin = System.currenttimemillis ();
for (int i = 0; i < n; i++) {
oo.add (i); 1
oo.add (0, i); 2
oo.add (I/2, i); 3
}
System.out.println (Oo.getclass (). GetName () + ": I am Adding method:" + (System.currenttimemillis ()-begin);
Return oo;
}
results: I found that if you call in main, the sequence has some effect on the result, so the use of multithreading calls to ensure fairFirst run: Second run: Third run: java.util.ArrayList: I am Add method: java.util.ArrayList: I am Add method: java.util.LinkedList: I am Add method: 43
Java.util.Vector: I am adding method: Java.util.Vector: I am Add method: Java.util.Vector: I am Add method: 37
Java.util.LinkedList: I am adding a method: java.util.LinkedList: I am Add method: java.util.LinkedList: I am Add method: 40
Conclusion: It can be seen that for the tail add operation, the performance of three kinds of lists is almost the same, can also change the N size, the result consumption time is almost linear growth (O (N)). When n is too large to keep O (n), I personally think it is the cause of my machine.
2. The head add operation: The 1 operation of the MakeList1 Method 1, 3 comments, eliminate 2 commentsn=2000 run: n=20000 run: n=200000 run: java.util.ArrayList: I am Add method: 2 Java.util.Array List: I am adding method: java.util.ArrayList: I am adding a method: 5274
Java.util.LinkedList: I was adding a method: 1 java.util.LinkedList: I was adding a method: 5 java.util.LinkedList: I was adding the method: 25
Java.util.Vector: I was adding a method: 3 java.util.Vector: I was adding a method: Java.util.Vector: I was adding a method: 5285
Conclusion: The LinkedList consumption time of the head operation remained linearly increased (O (N)), but Arraylist,vector almost (O (n^2)) grew.
3. The middle operation: Will MakeList1 Method 1, 2 annotation, eliminates 3 's annotationn=2000 run: n=20000 run: n=80000 run: java.util.ArrayList: I am adding method: 1 java.util.ArrayList: I am adding method: java.util.ArrayList: I am adding a method: 667
Java.util.Vector: I was adding a method: 1 Java.util.Vector: I was adding a method: Java.util.Vector: I was adding a method: 670
Java.util.LinkedList: I was adding a method: 5 java.util.LinkedList: I was adding a method: 294 java.util.LinkedList: I was adding a method: 27950
Conclusion: The result is that I have some surprises, the reason is that in the middle of add linkedlist to go through the search for index, and ArrayList just to move the general elements backward. Therefore, LinkedList only in front of the performance operation ArrayList;
4.get Operation:
<pre name= "code" class= "java" >public static void GetList1 (List<integer> oo) {
int total = 0;
Long begin = System.currenttimemillis ();
for (int i = 0; i < oo.size (); i++) {total
+ = Oo.get (i);
System.out.println (Oo.getclass (). GetName () + ": Get run Time:" + (System.currenttimemillis ()-Begin) + "result" + total); c8/>}
GetList1 (MakeList1 (List2, 20000)); The function in the//run method is changed to this
public static void GetList1 (List<integer> oo) {
int. total = 0;
iterator<integer> ITR = Oo.iterator ();
Long begin = System.currenttimemillis ();
while (Itr.hasnext ()) {total
+ = Itr.next ();
}
System.out.println (Oo.getclass (). GetName () + ": Get run Time:" + (System.currenttimemillis ()-Begin) + "result" + total);
}
n=20000 run: n=60000 run: java.util.ArrayList: I am Add method: 2 java.util.ArrayList: I am Add method: 10
Java.util.ArrayList:get Run time: 2 results 199990000 Java.util.ArrayList:get run time: 2 results 1799970000
Java.util.LinkedList: I am adding the method: 5 java.util.LinkedList: I am Add method: 6
Java.util.LinkedList:get Run Time: 176 results 199990000 Java.util.LinkedList:get run time: 1534 results 1799970000
Java.util.Vector: I am adding the method: 3 java.util.Vector: I am Add method: 8
Java.util.Vector:get Run time: 2 results 199990000 Java.util.Vector:get run time: 6 results 1799970000
When using the following GetList1 method, the performance of the three is almost error, interested can try
Conclusion: ArrayList also had vector spending time basically remained linear, LinkedList O (n^2) growth, set operation and individual operation results are basically the same.
5. Traverse Remove Operation:
public static void RemoveList1 (List<integer> oo) {
iterator<integer> ITR = Oo.iterator ();
Long begin = System.currenttimemillis ();
while (Itr.hasnext ()) {
if (Itr.next ()% 3 = 0) itr.remove ();
}
System.out.println (Oo.getclass (). GetName () + "I am Delete method:" + (System.currenttimemillis ()-Begin);
}
</pre><pre name= "code" class= "Java" >removelist1 (MakeList1 (List1, 300000))//Change the Run method contents to this
n=60000 run: n=600000 run: java.util.LinkedList I was Delete method: 9 java.util.LinkedList I was Delete method: 17
Java.util.ArrayList I am Delete method: 134 java.util.ArrayList I am Delete method: 3797
Java.util.Vector I am Delete method: 139 Java.util.Vector I am Delete method: 3802
Conclusion: LinkedList operation time remained linear, Vector,arraylist was O (n^2)
6. Random Remove Operation:
public static void RemoveList1 (List<integer> oo) {
int n = oo.size ()/2;
Long begin = System.currenttimemillis ();
for (int i = n; n > 0; n--) {
oo.remove (i);
}
System.out.println (Oo.getclass (). GetName () + "I am Delete method:" + (System.currenttimemillis ()-Begin);
}
n=3000 run: n=9000 run: java.util.ArrayList I was Delete method: 1 java.util.ArrayList I was Delete method: 4
Java.util.Vector I was Delete method: 1 Java.util.Vector I was deleted by: 3
Java.util.LinkedList I was Delete method: 5 java.util.LinkedList I was deleted by: 20
Conclusion: At this time LinkedList performance is not as good as Arraylist,vector
Summary:
comparing the above data, it is found that linkedlist performance is always inferior to arraylist,vector as long as it is in random operation. In the case of non random (iterator traversal) LinkedList is increasing, the deletion performance performance is better than Arraylist,vector
So, I think you should use ArrayList or vectors when you want to do random data at times. Use LinkedList when you do not need to operate at all times
Found a problem, why in my practice vector is not much worse than ArrayList performance ... Is it because I use multithreading.