1 实例测试代码
// 1 ArrayList set sort elements and list elements
public static void printArrList() {
List listLined = new ArrayList();
long st3 = System.currentTimeMillis();
for (int x = 0; x < 1000000; x++) {
listLined.add(String.valueOf(x));
}
for (int m = 0; m < listLined.size(); m++) {
String tempString = listLined.get(m);
}
long st4 = System.currentTimeMillis();
System.out.println("arrayList Get Elements time By normal =" + (st4 - st3));
}
// 2 LinedkList set sort elements and list elements
public static void printLinkedList() {
List listLined = new LinkedList();
long st3 = System.currentTimeMillis();
for (int x = 0; x < 100000; x++) {
listLined.add(String.valueOf(x));
}
for (int m = 0; m < listLined.size(); m++) {
String tempString = listLined.get(m);
}
long st4 = System.currentTimeMillis();
System.out.println("LinkedList Get Elements time By normal =" + (st4 - st3));
}
// 3 ArrayList set sort elements and list elements By FOR
public static void printArrListByFor() {
List listLined = new ArrayList();
long st3 = System.currentTimeMillis();
for (int x = 0; x < 1000000; x++) {
listLined.add(String.valueOf(x));
}
for (String m : listLined) {
String tempString = m;
}
long st4 = System.currentTimeMillis();
System.out.println("arrayList Get Elements By FOR with time="
+ (st4 - st3));
}
// 4 LinkedList set sort elements and list elements By FOR
public static void printLinkedListByFor() {
List listLined = new LinkedList();
long st3 = System.currentTimeMillis();
for (int x = 0; x < 1000000; x++) {
listLined.add(String.valueOf(x));
}
for (String m : listLined) {
String tempString = m;
}
long st4 = System.currentTimeMillis();
System.out.println("LinkedList Get Elements By FOR with time="
+ (st4 - st3));
}
// 5 ArrayList set sort elements and list elements By Itorator
public static void printArrListByItorator() {
List listLined = new ArrayList();
long st3 = System.currentTimeMillis();
for (int x = 0; x < 1000000; x++) {
listLined.add(String.valueOf(x));
}
Iterator iterator = listLined.iterator();
while( iterator.hasNext() ){
String tempString = iterator.next();
}
long st4 = System.currentTimeMillis();
System.out.println("arrayList Get Elements By Itorator with time="
+ (st4 - st3));
}
// 6 LinkedList set sort elements and list elements By Itorator
public static void printLinkedListByItorator() {
List listLined = new LinkedList();
long st3 = System.currentTimeMillis();
for (int x = 0; x < 1000000; x++) {
listLined.add(String.valueOf(x));
}
Iterator iterator = listLined.iterator();
while( iterator.hasNext() ){
String tempString = iterator.next();
}
long st4 = System.currentTimeMillis();
System.out.println("LinkedList Get Elements By Itorator with time="
+ (st4 - st3));
}
public static void main(String[] args) {
for(int x = 1; x <= 5 ; x++ ){
System.out.println(" loop test time:" + x);
printArrList();
printLinkedList();
printArrListByFor();
printLinkedListByFor();
printArrListByItorator();
printLinkedListByItorator();
}
}
2 3次输出结果
loop test time:1
arrayList Get Elements time By normal =184
LinkedList Get Elements time By normal =7434
arrayList Get Elements By FOR with time=218
LinkedList Get Elements By FOR with time=138
arrayList Get Elements By Itorator with time=231
LinkedList Get Elements By Itorator with time=92
loop test time:2
arrayList Get Elements time By normal =98
LinkedList Get Elements time By normal =7428
arrayList Get Elements By FOR with time=185
LinkedList Get Elements By FOR with time=62
arrayList Get Elements By Itorator with time=92
LinkedList Get Elements By Itorator with time=180
loop test time:3
arrayList Get Elements time By normal =43
LinkedList Get Elements time By normal =8004
arrayList Get Elements By FOR with time=104
LinkedList Get Elements By FOR with time=64
arrayList Get Elements By Itorator with time=76
LinkedList Get Elements By Itorator with time=51
loop test time:4
arrayList Get Elements time By normal =47
LinkedList Get Elements time By normal =7490
arrayList Get Elements By FOR with time=109
LinkedList Get Elements By FOR with time=64
arrayList Get Elements By Itorator with time=99
LinkedList Get Elements By Itorator with time=65
loop test time:5
arrayList Get Elements time By normal =62
LinkedList Get Elements time By normal =7890
arrayList Get Elements By FOR with time=44
LinkedList Get Elements By FOR with time=57
arrayList Get Elements By Itorator with time=84
LinkedList Get Elements By Itorator with time=49
注意:1printArrList(); printArrListByFor(); printLinkedListByFor();printArrListByItorator();处于千万量级别,
2 printLinkedList()方法只能处于十万级别量, printLinkedListByItorator()只能处于百万量级别,千万量级别会报:java.lang.OutOfMemoryError: Java heap space
3 结论
1. ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
2. 对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
3. 对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据 ,但在尾部新增和删除除外。
4. For(foreach)、Iterator原理都是基于迭代器,区别不大,在获取元素的时候的时间复杂度是 O(1) (使用了 getNext() 和 hasNext() 方法),最终的时间复杂度为 O(n), 并且两者有迭代器的内存开销;
而LinkedList循环里每次在调用 get(i) 的时候花费的时间复杂度为 O(n),最终整个循环的时间复杂度就是 O(n^2);
ArrayList循环里每次在调用 get(i) 方法的时间复杂度就是 O(1),最终整个循环的时间复杂度就是 O(n);
4 建议
如果不考虑内存空间:使用普通的ArrayList 的Iterator 方法遍历,为最优方案
考虑内存空间:使用普通的ArrayList,并for遍历为最优方案