ArrayList和LinkedList效率问题,一个demo看懂?
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<String> al = new ArrayList<String>();
List<String> ll = new LinkedList<String>();
int i = 0;
while(i<10000000) {
al.add(String.valueOf(i));
ll.add(String.valueOf(i));
i++;
}
int size = al.size()-1;
// 比较第一位add
long currentTimeMillis = System.currentTimeMillis();
al.add(0, "1");
System.out.println("array Let's start with the first one add: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.add(0, "1");
System.out.println("linked Let's start with the first one add: " + (System.currentTimeMillis() - currentTimeMillis));
// 比较第一位set
currentTimeMillis = System.currentTimeMillis();
al.set(0, "1");
System.out.println("array Let's start with the first one set: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.set(0, "1");
System.out.println("linked Let's start with the first one set: " + (System.currentTimeMillis() - currentTimeMillis));
// 比较第一位del
currentTimeMillis = System.currentTimeMillis();
al.remove(0);
System.out.println("array Let's start with the first one del: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.remove(0);
System.out.println("linked Let's start with the first one del: " + (System.currentTimeMillis() - currentTimeMillis));
// 比较最后一位add
currentTimeMillis = System.currentTimeMillis();
al.add("1");
System.out.println("array Let's start with the last one add: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.add("1");
System.out.println("linked Let's start with the last one add: " + (System.currentTimeMillis() - currentTimeMillis));
// 比较最后一位set
currentTimeMillis = System.currentTimeMillis();
al.set(size, "1");
System.out.println("array Let's start with the last one set: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.set(size, "1");
System.out.println("linked Let's start with the last one set: " + (System.currentTimeMillis() - currentTimeMillis));
// 比较最后一位del
currentTimeMillis = System.currentTimeMillis();
al.remove(size);
System.out.println("array Let's start with the last one del: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.remove(size);
System.out.println("linked Let's start with the last one del: " + (System.currentTimeMillis() - currentTimeMillis));
// 比较1/3处add
size = size/3;
currentTimeMillis = System.currentTimeMillis();
al.add(size, "1");
System.out.println("array Let's start with the " + size + " add: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.add(size, "1");
System.out.println("linked Let's start with the " + size + " add: " + (System.currentTimeMillis() - currentTimeMillis));
// 比较1/3处set
currentTimeMillis = System.currentTimeMillis();
al.set(size, "1");
System.out.println("array Let's start with the " + size + " set: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.set(size, "1");
System.out.println("linked Let's start with the " + size + " set: " + (System.currentTimeMillis() - currentTimeMillis));
// 比较1/3处del
currentTimeMillis = System.currentTimeMillis();
al.remove(size);
System.out.println("array Let's start with the " + size + " del: " + (System.currentTimeMillis() - currentTimeMillis));
currentTimeMillis = System.currentTimeMillis();
ll.remove(size);
System.out.println("linked Let's start with the " + size + " del: " + (System.currentTimeMillis() - currentTimeMillis));
}
}
array Let's start with the first one add: 7
linked Let's start with the first one add: 0
array Let's start with the first one set: 0
linked Let's start with the first one set: 0
array Let's start with the first one del: 8
linked Let's start with the first one del: 0
array Let's start with the last one add: 0
linked Let's start with the last one add: 0
array Let's start with the last one set: 0
linked Let's start with the last one set: 0
array Let's start with the last one del: 0
linked Let's start with the last one del: 0
array Let's start with the 6666666 add: 2
linked Let's start with the 6666666 add: 33
array Let's start with the 6666666 set: 0
linked Let's start with the 6666666 set: 33
array Let's start with the 6666666 del: 3
linked Let's start with the 6666666 del: 35
友情提示:三分之一处的比较是个坑。