java集合类List
1.List
Vector:线程安全的。
ArrayList:适合查找与顺序添加。
LinkedList:适合随机插入与删除。
1.1ArrayList与LinkedList的add添加
1.1.1ArrayList与LinkedList的顺序add添加
代码:
1 List<String> linkedList = new LinkedList<>(); 2 List<String> arrayList = new ArrayList<>(); 3 4 Long startTime = System.currentTimeMillis(); 5 for (int i=0; i<1000000; i++) { 6 linkedList.add("linkedList"+i); 7 } 8 Long endTime = System.currentTimeMillis(); 9 System.out.println("linkedList:"+(endTime-startTime)); 10 11 startTime=0L;endTime=0L; 12 13 startTime = System.currentTimeMillis(); 14 for (int i=0;i<1000000; i++) { 15 arrayList.add("arrayList"+i); 16 } 17 endTime=System.currentTimeMillis(); 18 System.out.println("arrayList:"+(endTime-startTime));
运行结果:
1 linkedList:340ms 2 arrayList:812ms
可以看出顺序添加ArrayList比LinkedList快
1.1.2ArrayList与LinkedList的add插入添加
1 List<String> linkedList = new LinkedList<>(); 2 List<String> arrayList = new ArrayList<>(); 3 4 Long startTime = System.currentTimeMillis(); 5 6 for (int i=0;i<1000000; i++) { 7 arrayList.add(0,"arrayList"+i); 8 } 9 Long endTime=System.currentTimeMillis(); 10 System.out.println("arrayList:"+(endTime-startTime)+"ms"); 11 12 startTime=0L;endTime=0L; 13 14 startTime = System.currentTimeMillis(); 15 for (int i=0; i<1000000; i++) { 16 linkedList.add(0,"linkedList"+i); 17 } 18 19 endTime=System.currentTimeMillis(); 20 System.out.println("linkedList:"+(endTime-startTime)+"ms");
运行结果:
arrayList:127991ms
linkedList:1038ms
可以看出插入添加LinkedList比ArrayList快得多
1.2ArrayList与LinkedLis的set修改比较
代码:
1 List<String> linkedList = new LinkedList<>(); 2 List<String> arrayList = new ArrayList<>(); 3 4 5 for (int i=0;i<10000; i++) { 6 arrayList.add(i/2,"arrayList"+i); 7 } 8 for (int i=0; i<10000; i++) { 9 linkedList.add(i/2,"linkedList"+i); 10 } 11 12 13 Long startTime = System.currentTimeMillis(); 14 for (int i=0; i<10000; i++) { 15 linkedList.set(i,"linkedListNew"+i); 16 } 17 Long endTime=System.currentTimeMillis(); 18 System.out.println("arrayList:"+(endTime-startTime)+"ms"); 19 20 startTime=0L;endTime=0L; 21 22 startTime = System.currentTimeMillis(); 23 for (int i=0; i<10000; i++) { 24 arrayList.set(i,"linkedListNew"+i); 25 } 26 endTime=System.currentTimeMillis(); 27 System.out.println("linkedList:"+(endTime-startTime)+"ms");
运行结果:
1 arrayList:417ms 2 linkedList:5ms
可以看出set修改LinkedList比ArrayList快的多
1.3ArrayList与LinkedLis的remove比较
1.3.1 ArrayList与LinkedLis的remove(0)比较
代码:
List<String> linkedList = new LinkedList<>(); List<String> arrayList = new ArrayList<>(); for (int i=0;i<10000; i++) { arrayList.add(i/2,"arrayList"+i); } for (int i=0; i<10000; i++) { linkedList.add(i/2,"linkedList"+i); } Long startTime = System.currentTimeMillis(); for (int i=0; i<10000; i++) { linkedList.remove(0); } Long endTime=System.currentTimeMillis(); System.out.println("linkedList:"+(endTime-startTime)+"ms"); startTime=0L;endTime=0L; startTime = System.currentTimeMillis(); for (int i=0; i<10000; i++) { arrayList.remove(0); } endTime=System.currentTimeMillis(); System.out.println("arrayList:"+(endTime-startTime)+"ms");
运行结果:
1 linkedList:2ms 2 arrayList:11ms
可以看出两者都很快但LinkedList比ArrayList更快
1.3.2ArrayList与LinkedLis的0位置删除比较
代码:
1 List<String> linkedList = new LinkedList<>(); 2 List<String> arrayList = new ArrayList<>(); 3 4 for (int i=0;i<1000000; i++) { 5 arrayList.add("arrayList"+i); 6 } 7 for (int i=0; i<1000000; i++) { 8 linkedList.add("linkedList"+i); 9 } 10 Long startTime = System.currentTimeMillis(); 11 for(int i=0; i< 1000000; i++){ 12 arrayList.remove(0); 13 } 14 Long endTime=System.currentTimeMillis(); 15 System.out.println("arrayList:"+(endTime-startTime)+"ms"); 16 17 startTime=0L;endTime=0L; 18 19 startTime = System.currentTimeMillis(); 20 21 for(int i=0; i< 1000000; i++){ 22 linkedList.remove(0); 23 } 24 endTime=System.currentTimeMillis(); 25 System.out.println("linkedList:"+(endTime-startTime)+"ms");
运行结果
1 arrayList:129623ms 2 linkedList:26ms