参考http://how2j.cn/k/collection/collection-arraylist-vs-linkedlist/690.html#nowhere
ArrayList和LinkedList的区别
ArrayList 插入,删除数据慢
LinkedList, 插入,删除数据快
ArrayList是顺序结构,所以定位很快,指哪找哪。 就像电影院位置一样,有了电影票,一下就找到位置了。
LinkedList 是链表结构,就像手里的一串佛珠,要找出第99个佛珠,必须得一个一个的数过去,所以定位慢
插入数据(最前面插入数据)
package collection; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class TestCollection { public static void main(String[] args) { List<Integer> l; l = new ArrayList<>(); insertFirst(l, "ArrayList"); l = new LinkedList<>(); insertFirst(l, "LinkedList"); } private static void insertFirst(List<Integer> l, String type) { int total = 1000 * 100; final int number = 5; long start = System.currentTimeMillis(); for (int i = 0; i < total; i++) { l.add(0, number); } long end = System.currentTimeMillis(); System.out.printf("在%s 最前面插入%d条数据,总共耗时 %d 毫秒 %n", type, total, end - start); } }
由此可见,LinkedList插入数据的速度比ArrayList插入数据的速度快了110倍。
插入数据(最后插入数据)
package swordOffer;
import java.util.*;
public class test {
public static void main(String[] args) {
List<Integer> l;
l = new ArrayList<>();
insertFirst(l, "ArrayList");
l = new LinkedList<>();
insertFirst(l, "LinkedList");
}
private static void insertFirst(List<Integer> l, String type) {
int total = 1000 * 100;
final int number = 5;
long start = System.currentTimeMillis();
for (int i = 0; i < total; i++) {
l.add(number);
}
long end = System.currentTimeMillis();
System.out.printf("在%s 最后插入%d条数据,总共耗时 %d 毫秒 %n", type, total, end - start);
}
}
import java.util.*;
public class test {
public static void main(String[] args) {
List<Integer> l;
l = new ArrayList<>();
insertFirst(l, "ArrayList");
l = new LinkedList<>();
insertFirst(l, "LinkedList");
}
private static void insertFirst(List<Integer> l, String type) {
int total = 1000 * 100;
final int number = 5;
long start = System.currentTimeMillis();
for (int i = 0; i < total; i++) {
l.add(number);
}
long end = System.currentTimeMillis();
System.out.printf("在%s 最后插入%d条数据,总共耗时 %d 毫秒 %n", type, total, end - start);
}
}
由此可见,LinkedList插入数据的速度比ArrayList插入数据的速度快了差不多倍。
插入数据(从中间插入数据)
package swordOffer; import java.util.*; public class test { public static void main(String[] args) { List<Integer> l; l = new ArrayList<>(); insertFirst(l, "ArrayList"); l = new LinkedList<>(); insertFirst(l, "LinkedList"); } private static void insertFirst(List<Integer> l, String type) { int total = 1000 * 100; final int number = 5; long start = System.currentTimeMillis(); for (int i = 0; i < total; i++) { l.add(l.size()/2,number); } long end = System.currentTimeMillis(); System.out.printf("在%s 最中间插入%d条数据,总共耗时 %d 毫秒 %n", type, total, end - start); } }
由此可见,ArrayList中间插入数据的速度比LinkedList中间插入数据的速度快了400倍。
定位数据
package collection;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
List<Integer> l;
l = new ArrayList<>();
modify(l, "ArrayList");
l = new LinkedList<>();
modify(l, "LinkedList");
}
private static void modify(List<Integer> l, String type) {
int total = 100 * 1000;
int index = total/2;
final int number = 5;
//初始化
for (int i = 0; i < total; i++) {
l.add(number);
}
long start = System.currentTimeMillis();
for (int i = 0; i < total; i++) {
int n = l.get(index);
n++;
l.set(index, n);
}
long end = System.currentTimeMillis();
System.out.printf("%s总长度是%d,定位到第%d个数据,取出来,加1,再放回去%n 重复%d遍,总共耗时 %d 毫秒 %n", type,total, index,total, end - start);
System.out.println();
}
}
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class TestCollection {
public static void main(String[] args) {
List<Integer> l;
l = new ArrayList<>();
modify(l, "ArrayList");
l = new LinkedList<>();
modify(l, "LinkedList");
}
private static void modify(List<Integer> l, String type) {
int total = 100 * 1000;
int index = total/2;
final int number = 5;
//初始化
for (int i = 0; i < total; i++) {
l.add(number);
}
long start = System.currentTimeMillis();
for (int i = 0; i < total; i++) {
int n = l.get(index);
n++;
l.set(index, n);
}
long end = System.currentTimeMillis();
System.out.printf("%s总长度是%d,定位到第%d个数据,取出来,加1,再放回去%n 重复%d遍,总共耗时 %d 毫秒 %n", type,total, index,total, end - start);
System.out.println();
}
}
package collection; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class TestCollection { public static void main(String[] args) { List<Integer> l; l = new ArrayList<>(); modify(l, "ArrayList"); l = new LinkedList<>(); modify(l, "LinkedList"); } private static void modify(List<Integer> l, String type) { int total = 100 * 1000; int index = total/2; final int number = 5; //初始化 for (int i = 0; i < total; i++) { l.add(number); } long start = System.currentTimeMillis(); for (int i = 0; i < total; i++) { int n = l.get(index); n++; l.set(index, n); } long end = System.currentTimeMillis(); System.out.printf("%s总长度是%d,定位到第%d个数据,取出来,加1,再放回去%n 重复%d遍,总共耗时 %d 毫秒 %n", type,total, index,total, end - start); System.out.println(); }
由此可见,ArrayList定位数据的速度比LinkedList定位数据的速度快了2200倍。