码迷,mamicode.com
首页 > 其他好文 > 详细

Arraylist、vector、Linkedlist的比较

时间:2015-05-03 11:52:57      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

? ?

总结

? ?

首先它们的底层实现不同,Arraylistvector都是基于数组实现的,而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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!