想看我更多文章:【张旭童的博客】blog.csdn.net/zxt0601
想来gayhub和我gaygayup:【mcxtzhang的Github主页】github.com/mcxtzhangjava
本篇是Java集合类解析的第二篇,上一篇[面试必备:ArrayList源码解析(JDK8)]里,咱们唠了ArrayList
,今儿来继续说LinkedList
.面试中,这兄弟俩也常常会拿来比较。node
它们两能够说是List
接口的两种不一样的实现,ArrayList
的增删效率低,可是改查效率高。
而LinkedList
正好相反,增删因为不须要移动底层数组数据,其底层是链表实现的,只须要修改链表节点指针,因此效率较高。
而改和查,都须要先定位到目标节点,因此效率较低。git
开篇前,再说一遍Collection.toArray();
。
这个方法很重要,不论是ArrayList
、LinkedList
在批量add的时候,都会先转化成数组去作。 由于数组能够用for循环直接花式遍历。比较方便 高效github
套路依旧,
本文将从几个经常使用方法下手,来阅读LinkedList
的源码。
按照从构造方法->经常使用API(增、删、改、查)的顺序来阅读源码,并会讲解阅读方法中涉及的一些变量的意义。了解LinkedList
的特色、适用场景。面试
若是本文中有不正确的结论、说法,请你们提出和我讨论,共同进步,谢谢。数组
归纳的说,LinkedList
是线程不安全的,容许元素为null的双向链表。
其底层数据结构是链表,它实现List<E>, Deque<E>, Cloneable, java.io.Serializable
接口,它实现了Deque<E>
,因此它也能够做为一个双端队列。和ArrayList
比,没有实现RandomAccess
因此其如下标,随机访问元素速度较慢。安全
因其底层数据结构是链表,因此可想而知,它的增删只须要移动指针便可,故时间效率较高。不须要批量扩容,也不须要预留空间,因此空间效率比ArrayList
高。markdown
缺点就是须要随机访问元素时,时间效率很低,虽然底层在根据下标查询Node的时候,会根据index判断目标Node在前半段仍是后半段,而后决定是顺序仍是逆序查询,以提高时间效率。不过随着n的增大,整体时间效率依然很低。数据结构
当每次增、删时,都会修改modCount。app
//集合元素数量 transient int size = 0; //链表头节点 transient Node<E> first; //链表尾节点 transient Node<E> last; //啥都不干 public LinkedList() { } //调用 public boolean addAll(Collection<? extends E> c) 将集合c全部元素插入链表中 public LinkedList(Collection<? extends E> c) { this(); addAll(c); }复制代码
构造方法基本没干啥。
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;
}
}复制代码
能够看出,这是一个双向链表。
接上文,先说addAll
//addAll ,在尾部批量增长 public boolean addAll(Collection<? extends E> c) { return addAll(size, c);//以size为插入下标,插入集合c中全部元素 } //以index为插入下标,插入集合c中全部元素 public boolean addAll(int index, Collection<? extends E> c) { checkPositionIndex(index);//检查越界 [0,size] 闭区间 Object[] a = c.toArray();//拿到目标集合数组 int numNew = a.length;//新增元素的数量 if (numNew == 0)//若是新增元素数量为0,则不增长,并返回false return false; Node<E> pred, succ; //index节点的前置节点,后置节点 if (index == size) { //在链表尾部追加数据 succ = null; //size节点(队尾)的后置节点必定是null pred = last;//前置节点是队尾 } else { succ = node(index);//取出index节点,做为后置节点 pred = succ.prev; //前置节点是,index节点的前一个节点 } //链表批量增长,是靠for循环遍历原数组,依次执行插入节点操做。对比ArrayList是经过System.arraycopy完成批量增长的 for (Object o : a) {//遍历要添加的节点。 @SuppressWarnings("unchecked") E e = (E) o; Node<E> newNode = new Node<>(pred, e, null);//之前置节点 和 元素值e,构建new一个新节点, if (pred == null) //若是前置节点是空,说明是头结点 first = newNode; else//不然 前置节点的后置节点设置问新节点 pred.next = newNode; pred = newNode;//步进,当前的节点为前置节点了,为下次添加节点作准备 } if (succ == null) {//循环结束后,判断,若是后置节点是null。 说明此时是在队尾append的。 last = pred; //则设置尾节点 } else { pred.next = succ; // 不然是在队中插入的节点 ,更新前置节点 后置节点 succ.prev = pred; //更新后置节点的前置节点 } size += numNew; // 修改数量size modCount++; //修改modCount return true; } //根据index 查询出Node, Node<E> node(int index) { // assert isElementIndex(index); //经过下标获取某个node 的时候,(增、查 ),会根据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; } } private void checkPositionIndex(int index) { if (!isPositionIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private boolean isPositionIndex(int index) { return index >= 0 && index <= size; //插入时的检查,下标能够是size [0,size] }复制代码
小结:
//在尾部插入一个节点: add public boolean add(E e) { linkLast(e); return true; } //生成新节点 并插入到 链表尾部, 更新 last/first 节点。 void linkLast(E e) { final Node<E> l = last; //记录原尾部节点 final Node<E> newNode = new Node<>(l, e, null);//以原尾部节点为新节点的前置节点 last = newNode;//更新尾部节点 if (l == null)//若原链表为空链表,须要额外更新头结点 first = newNode; else//不然更新原尾节点的后置节点为如今的尾节点(新节点) l.next = newNode; size++;//修改size modCount++;//修改modCount }复制代码
//在指定下标,index处,插入一个节点 public void add(int index, E element) { checkPositionIndex(index);//检查下标是否越界[0,size] if (index == size)//在尾节点后插入 linkLast(element); else//在中间插入 linkBefore(element, node(index)); } //在succ节点前,插入一个新节点e void linkBefore(E e, Node<E> succ) { // assert succ != null; //保存后置节点的前置节点 final Node<E> pred = succ.prev; //之前置和后置节点和元素值e 构建一个新节点 final Node<E> newNode = new Node<>(pred, e, succ); //新节点new是原节点succ的前置节点 succ.prev = newNode; if (pred == null)//若是以前的前置节点是空,说明succ是原头结点。因此新节点是如今的头结点 first = newNode; else//不然构建前置节点的后置节点为new pred.next = newNode; size++;//修改数量 modCount++;//修改modCount }复制代码
小结:
//删:remove目标节点 public E remove(int index) { checkElementIndex(index);//检查是否越界 下标[0,size) return unlink(node(index));//从链表上删除某节点 } //从链表上删除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++; //修改modCount return element; //返回取出的元素值 } private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } //下标[0,size) private boolean isElementIndex(int index) { return index >= 0 && index < size; }复制代码
删除链表中的指定节点:
//由于要考虑 null元素,也是分状况遍历 public boolean remove(Object o) { if (o == null) {//若是要删除的是null节点(从remove和add 里 能够看出,容许元素为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; } //将节点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) {//前置节点为null, first = next;//则首节点为next } else {//不然 更新前置节点的后置节点 prev.next = next; x.prev = null;//记得将要删除节点的前置节点置null } //若是后置节点为null,说明是尾节点 if (next == null) { last = prev; } else {//不然更新 后置节点的前置节点 next.prev = prev; x.next = null;//记得删除节点的后置节点为null } //将删除节点的元素值置null,以便GC x.item = null; size--;//修改size modCount++;//修改modCount return element;//返回删除的元素值 }复制代码
删也必定会修改modCount。 按下标删,也是先根据index找到Node,而后去链表上unlink掉这个Node。 按元素删,会先去遍历链表寻找是否有该Node,考虑到容许null值,因此会遍历两遍,而后再去unlink它。
public E set(int index, E element) { checkElementIndex(index); //检查越界[0,size) Node<E> x = node(index);//取出对应的Node E oldVal = x.item;//保存旧值 供返回 x.item = element;//用新值覆盖旧值 return oldVal;//返回旧值 }复制代码
改也是先根据index找到Node,而后替换值,改不修改modCount
//根据index查询节点
public E get(int index) { checkElementIndex(index);//判断是否越界 [0,size) return node(index).item; //调用node()方法 取出 Node节点, }复制代码
//根据节点对象,查询下标
public int indexOf(Object o) { int index = 0; if (o == null) {//若是目标对象是null //遍历链表 for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else {////遍历链表 for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; }复制代码
//从尾至头遍历链表,找到目标元素值为o的节点
public int lastIndexOf(Object o) { int index = size; if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node<E> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; }复制代码
查不修改modCount
咱们也顺带看一下toArray()
.毕竟这是个高频的API
public Object[] toArray() { //new 一个新数组 而后遍历链表,将每一个元素存在数组里,返回 Object[] result = new Object[size]; int i = 0; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item; return result; }复制代码
LinkedList 是双向列表。
经过下标获取某个node 的时候,(add select),会根据index处于前半段仍是后半段 进行一个折半,以提高查询效率
删也必定会修改modCount。 按下标删,也是先根据index找到Node,而后去链表上unlink掉这个Node。 按元素删,会先去遍历链表寻找是否有该Node,若是有,去链表上unlink掉这个Node。