【深入理解java集合】-LinkedList实现原理

一、LinkeddList简介

1、LinkedList概述

LinkedList是一个一个继承于AbstractSequentialList,并实现了List接口和Deque接口的双端链表。

LinkedList底层的链表结构使它支持高效的插入和删除操作,另外它实现了Deque接口,使得LinkedList类也具有队列的特性(addFirst(),removeLast()…),它可以被当作堆栈、队列或双端队列进行操作。

LinkedList 实现了Cloneable接口,即覆盖了函数clone(),能克隆。

LinkedList不是线程安全的,如果想使LinkedList变成线程安全的,可以调用静态类Collections类中的synchronizedList方法。

ArrayList一样,类中的iterator()方法和listIterator()方法返回的iterators迭代器是fail-fast的:当某一个线程A通过iterator去遍历某集合的过程中,若该集合的内容被其他线程所改变了;那么线程A访问集合时,就会抛出ConcurrentModificationException异常,产生fail-fast事件,底层由modCount实现。

AbstractSequentialList 实现了get(int index)set(int index, E element)add(int index, E element) remove(int index)这些骨干性函数。降低了List接口的复杂度。这些接口都是随机访问List的,LinkedList是双向链表(链表数据结构本身不具备随机访问的特性);既然它继承于AbstractSequentialList,就相当于已经实现了“get(int index)这些接口

此外,我们若需要通过AbstractSequentialList自己实现一个列表,只需要扩展此类,并提供 listIterator() size() 方法的实现即可。若要实现不可修改的列表,则需要实现列表迭代器的 hasNextnexthasPreviousprevious index 方法即可。

2、内部结构分析

LinkedList底层的数据结构是基于双向循环链表的,且Fisrt之前还有一个虚拟头结点,其不存放数据,用于指向链表地址。

LinkedList类中定义了一个内部私有类Node来描述链表的节点:

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;
        }
}

这个类就代表双端链表的节点Node。这个类有三个属性,分别是前驱节点,本节点的值,后继结点。本节点实例保存业务数据,前驱节点保存(指向)前一个节点(对象)的位置信息和后继结点保存后一个节点(对象)位置信息。

二、LinkedList的源码分析

1、重要属性

//通过LinkedList实现的接口可知,其支持队列操作,双向列表操作,能被克隆,支持序列化
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    // LinkedList的大小(指其所含的元素个数)
    transient int size = 0;

    /**
     * 指向第一个节点
     * 不变的: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;

    /**
     * 指向最后一个节点
     * 不变的: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;

    ......
}

first 是双向链表的表头,它是双向链表节点所对应的类Node的实例

last 是双向链表的最后一个元素,它是双向链表节点所对应的类Node的实例

size 是双向链表中节点的个数。

2、构造方法

LinkedList提供了两种种方式的构造器,构造一个空列表、以及构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回的顺序排列的。

    //构建一个空列表
    public LinkedList() {
    }

    /**
     * 构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回的顺序排列的
     * @param  c 包含用于去构造LinkedList的元素的collection
     * @throws NullPointerException 如果指定的collection为空
     */
    //构建一个包含指定集合c的列表
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

3、添加元素

LinkedList提供了头插入addFirst(E e)、尾插入addLast(E e)、add(E e)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)、add(int index, E element)这些添加元素的方法。

   //头插入,在列表首部插入节点值e
    public void addFirst(E e) {
        linkFirst(e);
    }

    //头插入,将节点值为e的节点设置为链表首节点
    private void linkFirst(E e) {
        final Node<E> f = first;
        //构建一个prev值为null,节点值为e,next值为f的新节点newNode
        final Node<E> newNode = new Node<>(null, e, f); 
        //将newNode作为首节点
        first = newNode;
        //如果原首节点为null,即原链表为null,则链表尾节点也设置为newNode
        if (f == null)
            last = newNode; 
        else //否则,原首节点的prev设置为newNode
            f.prev = newNode;
        size++;  
        modCount++;
    }

    //尾插入,在列表尾部插入节点值e,该方法等价于add()
    public void addLast(E e) {
        linkLast(e);
    }

    //尾插入,在列表尾部插入节点值e
    public boolean add(E e) {
        linkLast(e);
        return true;
    }    

    //尾插入,即将节点值为e的节点设置为链表的尾节点
    void linkLast(E e) {
        final Node<E> l = last;
        //构建一个prev值为l,节点值为e,next值为null的新节点newNode
        final Node<E> newNode = new Node<>(l, e, null);
        //将newNode作为尾节点
        last = newNode;
        //如果原尾节点为null,即原链表为null,则链表首节点也设置为newNode
        if (l == null)
            first = newNode; 
        else  //否则,原尾节点的next设置为newNode
            l.next = newNode;
        size++;
        modCount++;
    }

    //中间插入,在非空节点succ之前插入节点值e
    void linkBefore(E e, Node<E> succ) {
        // assert succ != null;
        final Node<E> pred = succ.prev;
        //构建一个prev值为succ.prev,节点值为e,next值为succ的新节点newNode
        final Node<E> newNode = new Node<>(pred, e, succ);
        //设置newNode为succ的前节点
        succ.prev = newNode;
        //如果succ.prev为null,即如果succ为首节点,则将newNode设置为首节点
        if (pred == null)  
            first = newNode;
        else //如果succ不是首节点
            pred.next = newNode;
        size++;
        modCount++;
    }

    /**
     * 按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此链表的尾部
     * 如果指定的集合添加到链表的尾部的过程中,集合被修改,则该插入过程的后果是不确定的。
     * 一般这种情况发生在指定的集合为该链表的一部分,且其非空。
     * @throws NullPointerException 指定集合为null
     */
    public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }

    //从指定的位置开始,将指定collection中的所有元素插入到此链表中,新元素的顺序为指定    
      collection的迭代器所返回的元素顺序
    public boolean addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index); //index >= 0 && index <= size

        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew == 0) 
            return false;

        Node<E> pred, succ; //succ指向当前需要插入节点的位置,pred指向其前一个节点
        if (index == size) { //说明在列表尾部插入集合元素
            succ = null;
            pred = last;
        } else {
            succ = node(index); //得到索引index所对应的节点
            pred = succ.prev;
        }

        //指定collection中的所有元素依次插入到此链表中指定位置的过程
        for (Object o : a) {
            @SuppressWarnings("unchecked") E e = (E) o;
            //将元素值e,前继节点pred“封装”为一个新节点newNode
            Node<E> newNode = new Node<>(pred, e, null);
            if (pred == null)  //如果原链表为null,则新插入的节点作为链表首节点
                first = newNode; 
            else
                pred.next = newNode;
            pred = newNode;  //pred指针向后移动,指向下一个需插入节点位置的前一个节点
        }

        //集合元素插入完成后,与原链表index位置后面的子链表链接起来
        if (succ == null) { //说明之前是在列表尾部插入的集合元素
            last = pred;  //pred指向的是最后插入的那个节点
        } else { 
            pred.next = succ;
            succ.prev = pred;
        }

        size += numNew;
        modCount++;
        return true;
    }

    //将指定的元素(E element)插入到列表的指定位置(index)
    public void add(int index, E element) {
        checkPositionIndex(index); //index >= 0 && index <= size

        if (index == size) 
            linkLast(element); //尾插入
        else
            linkBefore(element, node(index));  //中间插入
    }

从源码中可以看出,同ArrayList一样,add(E e)和addAll(Collection c)都是末尾添加;add(int index, E element ),add(int index, Collection c)为前插,LinkedList每添加一个元素,只需要两个逻辑:保存元素,然后与前后继Node关联。

add()存在空链表添加,末尾添加,中间前插入三种情况,如下图所示

addAll()方法包括下面五个步骤:

  1. 检查index范围是否在size之内
  2. toArray()方法把集合的数据存到对象数组中
  3. 得到插入位置的前驱和后继节点
  4. 遍历集合,将数据作为pre插入到指定位置
  5. 集合元素插入完成后,与原链表index位置后面的子链表链接起来

注意:与数组不同,ArrayList和LinkedList的下表index控制由size决定,而不是length,范围为0<=index<=size,但当size=length时,即允许超下表+1访问,虽然符合向容器末尾(size+1)位置添加元素的思维,但在数组中就是下标越界,那它们如何处理下标越界的问题呢?ArrayList通过ensureCapacity(size+1)扩容,LinkedList链表存储结构本来都是一个个动态增长不存在越界的问题。

4、元素读取

LinkedList提供了获取头getFirst()、获取尾getLast()、contains(Object o)、get(int index)、indexOf(Object o)、lastIndexOf(Object o)这些查找元素的方法。

//返回列表首节点元素值
    public E getFirst() {
        final Node<E> f = first;
        if (f == null)  //如果首节点为null
            throw new NoSuchElementException();
        return f.item;
    }

    //返回列表尾节点元素值
    public E getLast() {
        final Node<E> l = last;
        if (l == null) //如果尾节点为null
            throw new NoSuchElementException();
        return l.item;
    }

    //判断列表中是否包含有元素值o,返回true当列表中至少存在一个元素值e,使得(o==null?e==null:o.equals(e))
    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }

    //返回指定索引处的元素值
    public E get(int index) {
        checkElementIndex(index); //index >= 0 && index < size
        return node(index).item;  //node(index)返回指定索引位置index处的节点
    }

    //返回指定索引位置的节点
    Node<E> node(int index) {
        // assert isElementIndex(index);
        //折半思想,当index < size/2时,从列表首节点向后查找
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {  //当index >= size/2时,从列表尾节点向前查找
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

    //正向查找,返回LinkedList中元素值Object o第一次出现的位置,如果元素不存在,则返回-1
    public int indexOf(Object o) {
        int index = 0;
        //由于LinkedList中允许存放null,因此下面通过两种情况来分别处理
        if (o == 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;
    }

    //逆向查找,返回LinkedList中元素值Object o最后一次出现的位置,如果元素不存在,则返回-1
    public int lastIndexOf(Object o) {
        int index = size;
        //由于LinkedList中允许存放null,因此下面通过两种情况来分别处理
        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;
    }

由LinkedList的类结构可以看出,LinkedList是AbstractSequentialList的子类。AbstractSequentialList 实现了get(int index)、set(int index, E element)、add(int index, E element) 和 remove(int index)这些随机访问的函数,那么LinkedList也实现了这些随机访问的接口。LinkedList具体是如何实现随机访问的?即,具体是如何定义index这个参数的?

在源码中,Node<E> node(int index)方法是得到索引index所指向的Node节点的,折半分区查找,也就是折半遍历,效率低于ArraryList的下标随机访问,具体实现为:

    //返回指定索引位置的节点
    Node<E> node(int index) {
        // assert isElementIndex(index);
        //折半思想(分块查找),当index < size/2时,从列表首节点向后查找
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {  //当index >= size/2时,从列表尾节点向前查找
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
      }

该方法返回双向链表中指定位置处的节点,而链表中是没有下标索引的,要指定位置出的元素,就要遍历该链表,从源码的实现中,我们看到这里有一个加速动作。源码中先将index与长度size的一半比较,如果index<size/2,就只从位置0往后遍历到位置index处,而如果 index>size/2,就只从位置size往前遍历到位置index处。这样可以减少一部分不必要的遍历。

5、元素删除:

LinkedList提供了头删除removeFirst()、尾删除removeLast()、remove(int index)、remove(Object o)、clear()这些删除元素的方法。

//移除首节点,并返回该节点的元素值
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

    //删除非空的首节点f
    private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; // help GC
        first = next; //将原首节点的next节点设置为首节点
        if (next == null)  //如果原链表只有一个节点,即原首节点,删除后,链表为null
            last = null;
        else  
            next.prev = null;
        size--;
        modCount++;
        return element;
    }

    //移除尾节点,并返回该节点的元素值
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }

    //删除非空的尾节点l
    private E unlinkLast(Node<E> l) {
        // assert l == last && l != null;
        final E element = l.item;
        final Node<E> prev = l.prev;
        l.item = null;
        l.prev = null; // help GC
        last = prev; //将原尾节点的prev节点设置为尾节点
        if (prev == null) //如果原链表只有一个节点,则删除后,链表为null
            first = null;
        else
            prev.next = null;
        size--;
        modCount++;
        return element;
    }

    //移除此列表中指定位置上的元素
    public E remove(int index) {
        checkElementIndex(index);  //index >= 0 && index < 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; // help GC
        size--;
        modCount++;
        return element;
    }

    //移除列表中首次出现的指定元素(如果存在),LinkedList中允许存放重复的元素
    public boolean remove(Object o) {
        //由于LinkedList中允许存放null,因此下面通过两种情况来分别处理
        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;
    }

    //清除列表中所有节点
    public void clear() {
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        //   more than one generation
        // - is sure to free memory even if there is a reachable Iterator
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }

LinkedList的删除实质上是重整前后继点的链接,然后置空Node对象(被删除对象),交由虚拟机GC处理。x.next = x.previous = null;x.element = null;交给gc完成资源回收,删除操作结束。与ArrayList比较而言,LinkedList的添加和删除动作不需要“移动”很多数据,从而效率更高。

6、修改元素

LinkedList提供了set(int index, E element)方法来修改指定索引上的值。

    //替换指定索引位置节点的元素值,并返回旧值
    public E set(int index, E element) {
        checkElementIndex(index); //index >= 0 && index < size
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

7.Queue操作

Queue操作提供了peek()、element()、poll()、remove()、offer(E e)这些方法。

    //获取但不移除此队列的头;如果此队列为空,则返回 null
    public E peek() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
    }

    //获取但不移除此队列的头;如果此队列为空,则抛出NoSuchElementException异常
    public E element() {
        return getFirst();
    }

    //获取并移除此队列的头,如果此队列为空,则返回 null
    public E poll() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    //获取并移除此队列的头,如果此队列为空,则抛出NoSuchElementException异常
    public E remove() {
        return removeFirst();
    }

    //将指定的元素值(E e)插入此列表末尾
    public boolean offer(E e) {
        return add(e);
    }

8.Deque(双端队列)操作

Deque操作提供了offerFirst(E e)、offerLast(E e)、peekFirst()、peekLast()、pollFirst()、pollLast()、push(E e)、pop()、removeFirstOccurrence(Object o)、removeLastOccurrence(Object o)这些方法。

//将指定的元素插入此双端队列的开头
    public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }

    //将指定的元素插入此双端队列的末尾
    public boolean offerLast(E e) {
        addLast(e);
        return true;
    }

    //获取,但不移除此双端队列的第一个元素;如果此双端队列为空,则返回 null
    public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }

    //获取,但不移除此双端队列的最后一个元素;如果此双端队列为空,则返回 null
    public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }

    //获取并移除此双端队列的第一个元素;如果此双端队列为空,则返回 null
    public E pollFirst() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    //获取并移除此双端队列的最后一个元素;如果此双端队列为空,则返回 null
    public E pollLast() {
        final Node<E> l = last;
        return (l == null) ? null : unlinkLast(l);
    }

    //将一个元素推入此双端队列所表示的堆栈(换句话说,此双端队列的头部)
    public void push(E e) {
        addFirst(e);
    }

    //从此双端队列所表示的堆栈中弹出一个元素(换句话说,移除并返回此双端队列的头部)
    public E pop() {
        return removeFirst();
    }

    //从此双端队列移除第一次出现的指定元素,如果列表中不包含次元素,则没有任何改变
    public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }

    //从此双端队列移除最后一次出现的指定元素,如果列表中不包含次元素,则没有任何改变
    public boolean removeLastOccurrence(Object o) {
        //由于LinkedList中允许存放null,因此下面通过两种情况来分别处理
        if (o == null) {
            for (Node<E> x = last; x != null; x = x.prev) { //逆向向前
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

Queue与LinkedList基础方法的区别在于对链表为空时的处理,是抛出异常还是返回null。例如获取头节点:

getFirst(),element(),peek(),peekFirst() 这四个获取头结点方法的区别在于对链表为空时的处理,是抛出异常还是返回null。其中getFirst() 和element() 方法将会在链表为空时,抛出异常。其中getFirst() 和element() 方法将会在链表为空时,抛出异常。element()方法的内部就是使用getFirst()实现的。它们会在链表为空时,抛出NoSuchElementException。

9、其他public方法

clone()、toArray()、toArray(T[] a)

    //返回此 LinkedList实例的浅拷贝
    public Object clone() {
        LinkedList<E> clone = superClone();

        // Put clone into "virgin" state
        clone.first = clone.last = null;
        clone.size = 0;
        clone.modCount = 0;

        // Initialize clone with our elements
        for (Node<E> x = first; x != null; x = x.next)
            clone.add(x.item);

        return clone;
    }

    //返回一个包含LinkedList中所有元素值的数组
    public Object[] toArray() {
        Object[] result = new Object[size];
        int i = 0;
        for (Node<E> x = first; x != null; x = x.next)
            result[i++] = x.item;
        return result;
    }

    //如果给定的参数数组长度足够,则将ArrayList中所有元素按序存放于参数数组中,并返回
    //如果给定的参数数组长度小于LinkedList的长度,则返回一个新分配的、长度等于LinkedList长度   
    //的、包含LinkedList中所有元素的新数组
    @SuppressWarnings("unchecked")
    public <T> T[] toArray(T[] a) {
        if (a.length < size)
            a = (T[])java.lang.reflect.Array.newInstance(
                                a.getClass().getComponentType(), size);
        int i = 0;
        Object[] result = a;
        for (Node<E> x = first; x != null; x = x.next)
            result[i++] = x.item;

        if (a.length > size)
            a[size] = null;

        return a;
    }

10、LikedList遍历方式

LinkedList支持多种遍历方式。建议不要采用随机访问的方式去遍历LinkedList,而采用逐个遍历的方式。

10.1 Iterator迭代器。
 

for(Iterator iter = list.iterator(); iter.hasNext();)
    iter.next();

10.2 ListIterator遍历
 

   ListIterator<String> lIter = list.listIterator();
  //顺向遍历
  while(lIter.hasNext()){
      System.out.println(lIter.next());
  }
  //逆向遍历
  while(lIter.hasPrevious()){
      System.out.println(lIter.previous());
  }

10.3 随机访问

int size = list.size();
for (int i=0; i<size; i++) {
    list.get(i);        
}

禁止用这种方式,相当于双重遍历

10.4 加强for循环遍历

for (Integer integ:list) ;

猜你喜欢

转载自blog.csdn.net/qq_42022528/article/details/82856759