标签:
? ?
总结
? ?
首先它们的底层实现不同,Arraylist和vector都是基于数组实现的,而Linkedlist是基于链表实现的
? ?
另外虽然vector和arraylist都同样是基于数组实现的,但vector的源码中有很多synchronized方法,说明是线程安全的,同步的,而Arraylist是线程不安全的,非同步的
? ?
由于加了很多synchronized方法,造成vector会慢
? ?
比如我同样往这两种数据结构中加入2,000,000个同样的字符串,比较其插入的时间
? ?
86 millis has passed when using Arraylist.
124 millis has passed when using Vector.
? ?
测试代码
? ?
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 has 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 has 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 has passed when using Vector. ");
}
? ?
public static void main(String[] args) {
ArraylistTest();
linkedlistTest();
vectorTest();
? ?
}
? ?
Arraylist、vector、Linkedlist的比较
标签:
原文地址:http://www.cnblogs.com/keedor/p/4473404.html