ArrayList:其实现为数组方式实现,很多面试者都考了实现方式(笔者曾经被多次问及ArrayList实现方式),他内部实现其实是数组方式实现
继承机构如下
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable其中他实现了RandomAccess,用来标记其支持快速(通常是固定时间)随机访问。如果要是访问多的话即用ArrayList
private transient Object[] elementData; //说明其实现方式是数组
LinkedList:其实现方式为实现了Deque,而Deque又继承自Queue(队列)。所以他主要用来队列中使用,其删除速度较ArrayList要快许多。
继承机构如下
public class LinkedList<E>
extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.SerializableVector:其主要是早起存在的list,他的继承方式和arrayList相同,同样是数组实现方式,只不过他的每个方法都被synchronized修饰,说明其类是线程安全的,而arrayList是非线程安全的,但是从java1.2开始就可以不需要用它了如果需要同步功能,只需要调用集合工具类Collections的synchronizedList(List<T> list)方法即可。
下面代码分析了LinkedList和ArrayList的读取效率,以及删除效率
import java.util.*;
/** * Created by StateGrace on 2014/10/21. */public class ReadList { public static void main(String[] args) { List<Integer> arrayList = new ArrayList(); long start = System.currentTimeMillis(); for (int i = 0 ;i<10000000;i++){ arrayList.add(i); } System.out.println("插入元素所用时间为:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); arrayList.add(9999,9999); System.out.println("指定位置插入元素:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); Iterator it = arrayList.iterator(); while(it.hasNext()){ it.next(); } System.out.println("迭代器所用时间为:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); for(Integer integer :arrayList){ integer.toString(); } System.out.println("foreach所用时间为:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); arrayList.get(999999); System.out.println("单个get所用时间:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); arrayList.remove(999999); arrayList.remove(11123); arrayList.remove(5475467); arrayList.remove(769); arrayList.remove(98089); arrayList.remove(234234); arrayList.remove(786768); arrayList.remove(25465); arrayList.remove(8956); arrayList.remove(124794); System.out.println("移除10个中间元素时间:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); for (int i = 0; i < arrayList.size(); i++) { arrayList.get(i); } System.out.println("for循环所用时间为:"+(System.currentTimeMillis()-start)); System.out.println("===============分割线===================="); List<Integer> linkedList = new LinkedList(); start = System.currentTimeMillis(); for (int i = 0 ;i < 10000000;i++){ //linkedList. linkedList.add(i); } System.out.println("插入元素所用时间为:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); linkedList.add(9999,9999); System.out.println("指定位置插入元素:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); Iterator iteror = linkedList.iterator(); while(iteror.hasNext()){ iteror.next(); } System.out.println("迭代器所用时间为:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); for(Integer integer :linkedList){ integer.toString(); } System.out.println("foreach所用时间为:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); linkedList.get(999999); System.out.println("单个get所用时间:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); linkedList.remove(999999); linkedList.remove(11123); linkedList.remove(5475467); linkedList.remove(769); linkedList.remove(98089); linkedList.remove(234234); linkedList.remove(786768); linkedList.remove(25465); linkedList.remove(8956); linkedList.remove(124794); System.out.println("移除10个中间元素时间:"+(System.currentTimeMillis()-start)); start = System.currentTimeMillis(); for (int i = 0; i < linkedList.size(); i++) { linkedList.get(i); } System.out.println("for循环所用时间为:"+(System.currentTimeMillis()-start)); //这个是同步list的代码 Collections.synchronizedList(linkedList); }}结果
插入元素所用时间为:868
指定位置插入元素:9迭代器所用时间为:18foreach所用时间为:1140单个get所用时间:0移除10个中间元素时间:88for循环所用时间为:28===============分割线====================插入元素所用时间为:1897指定位置插入元素:0迭代器所用时间为:104foreach所用时间为:1165单个get所用时间:11移除10个中间元素时间:51其中,执行for循环linkedList会慢到假死,所以尽量要用 iterator()方法去遍历。