本文的主要目录结构:html
对比上一篇的ArrayList介绍【传送门:Java8集合框架——ArrayList源码分析】,LinkedList因内部实现不一样,其元素的内部访问方式也不同。LinkedList的特色大体和ArrayList的比较以下:java
关注的问题点 | ArrayList相关结论 | LinkedList相关结论 |
是否容许空的元素node |
是 | 是 |
是否容许重复的元素 | 是 | 是 |
元素有序:读取数据和存放数据的顺序一致数组 |
是 | 是 |
是否线程安全 | 否 | 否 |
随机访问的效率 | 随机访问指定索引(数组的索引)的元素快 | 因须要进行遍历,随机访问指定索引的元素较慢,而利用双向链表的特性,能够从两端进行访问,会使得平均访问的元素减小一半 |
顺序添加元素的效率 | 在不涉及扩容时,顺序添加元素速度快;安全 当须要扩容时,涉及到元素的复制,相对较慢app |
顺序添加元素速度快,只是新建一个结点,而后添加结点连接;框架 |
删除和插入元素的效率 | 因涉及到复制和移动后续的元素,相对较慢函数 |
删除和插入涉及到遍历找到相关结点,所以会慢点,可是找到结点后的相关操做会比较快,因不涉及到元素的移动源码分析 |
LinkedList是以双向链表为基础实现列表的集合,内部实现结点中,有指向上一个结点 prev 和下一个结点next 的引用,即双向链表,而 E item 即为实际的存储元素,实现源码以下:ui
private static class Node<E> { E item; // 实际存储的元素 Node<E> next; // 下一个元素的引用 Node<E> prev; // 上一个元素的引用 Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
LinkedList内部有几个重要属性,好比说保存了元素个数、首结点引用和尾结点引用等,所以要得到LinkedList的首元素和尾元素元素也比较直接。
// 实际存储的元素个数 transient int size = 0; /** * Pointer to first node. * 第一个节点元素的引用 * Invariant: (first == null && last == null) || * (first.prev == null && first.item != null) */ transient Node<E> first; /** * Pointer to last node. * 最后一个节点元素的引用 * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */ transient Node<E> last;
一、空构造函数
/** * Constructs an empty list. */ public LinkedList() { }
二、带参构造函数:经过指定集合构建
/** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null * 若是指定集合为空,会抛出NPE */ public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
这个带参的构造函数其实比较好理解,经过指定集合构建的LinkedList,其实就是建立一个空的 LinkedList,而后把集合里面的全部元素添加进去。而添加集合元素调用的 addAll(c) ,实际上只是从指定索引处(这里是从末尾,即 size 处)添加全部的集合元素:addAll(size, c) 。
/** * Appends all of the elements in the specified collection to the end of * this list, in the order that they are returned by the specified * collection's iterator. The behavior of this operation is undefined if * the specified collection is modified while the operation is in * progress. (Note that this will occur if the specified collection is * this list, and it's nonempty.) * * 往LinkedList中添加指定集合的全部元素 * * @param c collection containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ public boolean addAll(Collection<? extends E> c) { // 从索引 size 处添加索引,即从末尾添加节点 return addAll(size, c); }
再来看看 addAll(size, c) 的具体实现:
/** * Inserts all of the elements in the specified collection into this * list, starting at the specified position. Shifts the element * currently at that position (if any) and any subsequent elements to * the right (increases their indices). The new elements will appear * in the list in the order that they are returned by the * specified collection's iterator. * * 往LinkedList的指定索引处添加指定集合的全部元素 * * @param index index at which to insert the first element * from the specified collection * @param c collection containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws IndexOutOfBoundsException {@inheritDoc} * @throws NullPointerException if the specified collection is null */ public boolean addAll(int index, Collection<? extends E> c) { // index 的边界检查 checkPositionIndex(index); Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) return false; Node<E> pred, succ; // index == size 表示从链表的末尾添加节点元素 if (index == size) { succ = null; pred = last; } else { // 查找指定索引的节点 // succ 记录了指定 index 处的索引节点,而 pred 则记录前一个节点 // 也便是链表断开处先后的2个节点,中间须要添加指定的集合元素 succ = node(index); pred = succ.prev; } for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; // 构造新节点, 记录的前节点 pred 恰好是这个要插入到链表的新节点的 prev 节点 Node<E> newNode = new Node<>(pred, e, null); // 空链表 if (pred == null) first = newNode; else pred.next = newNode; pred = newNode; } // succ == null 表示是从链表尾部添加节点,已经没有后续节点了 if (succ == null) { last = pred; } else { // 连接断开的节点 pred.next = succ; succ.prev = pred; } // 记录实际元素个数 size 和已修改次数 modCount size += numNew; modCount++; return true; } // 对下表索引进行边界检查 private void checkPositionIndex(int index) { if (!isPositionIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } /** * Tells if the argument is the index of a valid position for an * iterator or an add operation. * 实际上就是直接与内部属性 size 的比较, 即必须落在 0 ~ size 之间,由于这是链表的实际位置 */ private boolean isPositionIndex(int index) { return index >= 0 && index <= size; } /** * Returns the (non-null) Node at the specified element index. * 查找指定索引的节点 */ Node<E> node(int index) { // assert isElementIndex(index); // 利用双向链表的特性,看要查找的索引是落在前半部分仍是后半部分,而后顺着查或者逆着查,可节省遍历的时间 if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
下面经过图示和文字进行具体的说明:
List<String> arrayList = new ArrayList<>(); arrayList.add("01"); arrayList.add("02"); arrayList.add("03"); List<String> linkedList = new LinkedList<>(arrayList);
大体图示如上。
直接上源码:
/** * Appends the specified element to the end of this list. * * <p>This method is equivalent to {@link #addLast}. * * @param e element to be appended to this list * @return {@code true} (as specified by {@link Collection#add}) */ public boolean add(E e) { linkLast(e); // 顺序添加新元素,实际上是从链表最后添加1个节点 return true; } /** * Links e as last element. */ void linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); // 新建节点: prev 为以前的尾节点 last,单连接完成 last = newNode; // 新节点即为尾节点 if (l == null) first = newNode; // 原来的尾节点为 null,说明是空链表,须要记录首节点 else l.next = newNode; // 记录原尾节点的 next指向,完成双向连接 size++; // 记录实际元素个数 size 和已修改次数 modCount modCount++; }
这段源码其实挺好理解的,大意就是
/** * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any * subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { // index 的边界检查 checkPositionIndex(index); // index == size ,则至关于从最后添加元素 if (index == size) linkLast(element); else linkBefore(element, node(index)); } /** * Inserts element e before non-null Node succ. * 在指定节点前添加元素 */ void linkBefore(E e, Node<E> succ) { // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; // 说明从首节点处插入节点, index = 0 else pred.next = newNode; size++; modCount++; }
根据指定索引查找元素。这里使用了双向链表的特性,能够向前或者向后顺序查找,即判断索引落在前半部分(index < (size >> 1)),向后索引,落在后半部分,向前索引,这就能保证最多只要遍历一半,提升效率。
/** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { checkElementIndex(index); // index 的边界检查 return node(index).item; } /** * Returns the (non-null) Node at the specified element index. */ Node<E> node(int index) { // assert isElementIndex(index); // 利用双向链表的特性,看要查找的索引是落在前半部分仍是后半部分,而后顺着查或者逆着查,可节省遍历的时间 if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
和ArrayList相似,删除元素也有2种
指定元素删除,是根据元素顺序进行判断直到找到相应的元素的,所以效率不高,从这里也能够看到,元素能够为null,由于null(使用==)和实际元素(使用equals)的对等判断是不同的方法;指定索引也是须要先找到索引所对应的元素,而后再删除元素。
/** * Removes the first occurrence of the specified element from this list, * if it is present. If this list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns {@code true} if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * 移除指定元素 * * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element */ public boolean remove(Object o) { if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } /** * Removes the element at the specified position in this list. Shifts any * subsequent elements to the left (subtracts one from their indices). * Returns the element that was removed from the list. * 移除指定索引的元素 * * @param index the index of the element to be removed * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { checkElementIndex(index); return unlink(node(index)); }
从实现来看,最后删除元素都是直接调用了unlink(Node n)。
/** * Unlinks non-null node x. * 移除一个非空节点 */ E unlink(Node<E> x) { // assert x != null; final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev; // 移除的是首节点 if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } // 移除的是尾节点 if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; }
这里大体讲一下。说白了,就是结点引用的变动,一个图就能够说明,以下。
这样便完成了连接的变动。固然这里还考虑了该元素是不是首元素(首元素 first 下移一格为 next)、是不是尾元素(尾元素 last 上移一格为 prev)。以下3种场景(删除普通节点、删除首节点或者尾节点、删除惟一节点)图示:
能够到,这里也是引用了 node(index),要顺序找到相应的元素,因此比较慢,可是一旦找到元素,就只会变动相关结点信息,这部分操做仍是比较快的。
1 /** 2 * Replaces the element at the specified position in this list with the 3 * specified element. 4 * 5 * @param index index of the element to replace 6 * @param element element to be stored at the specified position 7 * @return the element previously at the specified position 8 * @throws IndexOutOfBoundsException {@inheritDoc} 9 */ 10 public E set(int index, E element) { 11 checkElementIndex(index); 12 Node<E> x = node(index); 13 E oldVal = x.item; 14 x.item = element; 15 return oldVal; 16 }
做为 List 的基本功能大体说明如上 。