在数据量为50w的时候测试数据
@Test
public void array() {
//ArrayList追加元素
long starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
arrayList.add(i);
}
long endtime = System.currentTimeMillis();
System.out.println("ArrayList追加元素:"+(endtime - starttime));
//初始化
arrayList = new ArrayList();
//ArrayList插入元素
starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
arrayList.add(0,i);
}
endtime = System.currentTimeMillis();
System.out.println("ArrayList插入元素:"+(endtime - starttime));
//for循环遍历ArrayList
starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
arrayList.get(i);
}
endtime = System.currentTimeMillis();
System.out.println("for循环遍历ArrayList:"+(endtime - starttime));
//迭代器遍历ArrayList
starttime = System.currentTimeMillis();
Iterator i1 = arrayList.iterator();
Object obj1;
while(i1.hasNext()) {
obj1 = i1.next();
}
endtime = System.currentTimeMillis();
System.out.println("迭代器遍历ArrayList:"+(endtime - starttime));
}
@Test
public void linked() {
//LinkedList追加元素
long starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
linkedList.add(i);
}
long endtime = System.currentTimeMillis();
System.out.println("LinkedList追加元素:"+(endtime - starttime));
//初始化
linkedList = new LinkedList();
//LinkedList插入元素
starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
linkedList.add(0,i);
}
endtime = System.currentTimeMillis();
System.out.println("LinkedList插入元素:"+(endtime - starttime));
//for循环遍历LinkedList
starttime = System.currentTimeMillis();
for (int i = 0; i < COUNT; i++) {
linkedList.get(i);
}
endtime = System.currentTimeMillis();
System.out.println("for循环遍历LinkedList:"+(endtime - starttime));
//迭代器遍历LinkedList
starttime = System.currentTimeMillis();
Iterator i2 = linkedList.iterator();
Object obj2;
while(i2.hasNext()) {
obj2 = i2.next();
}
endtime = System.currentTimeMillis();
System.out.println("迭代器遍历LinkedList:"+(endtime - starttime));
}
可以看出在进行追加(有序添加)、遍历的时候ArrayList的效率高于LinkedList,但是在无序插入的时候LinkedList效率高于ArrayList。
在做其余数据量测试的时候得出ArrayList通过for循环遍历效率要高于迭代器遍历,LinkedList迭代器遍历效率要远高于for循环遍历。