java LinkedList 插入 源码分析

日常开场吹牛

LinkedList作为List集合的一种实现类(如果需要了解集合的继承体系可以参考我另一篇文章《用几张图捋完集合的继承实现关系》),其中和ArrayList的底层实现方式的不同在于,ArrayList的底层是由数组来实现,那么这样的底层就决定了ArrayList的读取速度会比较快,毕竟访问的是一个连续的内存地址,但是带来的弊端就是查询和修改会比较麻烦,需要遍历,再进行对应的读取和位移。

这个时候LinkedList的优势便体现了出来,其底层由双向链表构成这点可以保证其修改,只需要针对指定节点的前后指向进行修改即可。

进入正题

我们进入LinkedList的类可以发现,这个类的构成很简单,成员变量只有前后Node跟这个list的size。跟想象中有点不一样,毕竟应该是有一个个的元素来保存的呀。

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    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
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }
...
}

在这里我们来看下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;
        }
    }

不得不佩服当年sun公司的工程师们,简洁,这才叫大道至简。那现在我们可以知道,其实每个Node节点,除了保存自己的数据item之外,都是持有了另外的两个对象,分别是前驱,后继。

看完这个构成后可以知道size大小就是一开始声明时候的0.我们先来关注下LinkedList#add方法,即末位添加,究竟干了些什么

    /**
     * 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);
        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);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

可以看到,add方法只是对外暴露的方法,真正执行时候交给linkLast方法。

  1. 首先会获取这个list对象内部的Node类型成员last,即末位节点,以该节点作为新插入元素的前驱节点并创建新节点
  2. 然后把新节点作为该list对象的最后一个节点
  3. 紧接处理原先的末位节点,第一个情况分析如果这个list本来就是一个空的链表,ok,我们把新节点作为首节点。如果链表内部已经有元素,那么,现在把原来的末位节点的后继指向新节点,完成链表修改
  4. 最后修改当前list的size,并记录该list对象被执行修改的次数

末位添加方法的操作步骤就相当于,目标节点创建后寻找前驱节点, 前驱节点存在就修改前驱节点的后继,指向目标节点

接着我们可以来看下指定位置插入的add方法,内容如下

public void add(int index, E element) {
        checkPositionIndex(index);
        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

private void checkPositionIndex(int index) {
        if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

private boolean isPositionIndex(int index) {
        return index >= 0 && index <= size;
    }

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;
        }
    }
/**
* 指位添加方法核心逻辑
*/
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;
        else
            pred.next = newNode;
        size++;
        modCount++;
    }
  1. 检查插入位置是否合法,毕竟不能插个-1进去或者插一个不存在的位置吧
  2. 如果是插入位置是末位,那还是上面我们末位添加的逻辑;反之,说明当前list内指定位置节点存在开始寻找
  3. 这里查找我们可以看到,for,马上想到遍历这个list的节点元素,并访问节点的后继以找到下一个节点,这就是链表,而双向链接则是当前节点知道自己的后继是后面的节点后,只需要保证后面节点知道其前驱是自己,就可以实现互通。但这里要注意,LinkedList插入时候寻找的点并不一定是从链表表头开始,而是根据目标位置和list中间下标的大小来确定寻找方向是要往更高位置寻找,还是更低位置寻找,这种方式我们称之为二分查找
  4. 找到目标节点后,还是创建新节点,告诉新节点其前驱为原位置节点的前驱pred,其后继来为原位置节点succ。
  5. 那在这个时候就会发现,除了原位置节点succ,还有新节点newNode的前驱都指向了原位置前驱节点pred,也就是说,newNode知道自己的前后,而succ也节点知道的前后,最麻烦的是他们都说自己的前面是同一个节点,而且pred现在只知道自己的后继是succ,不认newNode
  6. 接下来要做的,就是告诉原位置节点,你现在的前面是新节点newNode,这样做,只是让原节点可以通过前驱来找到newNode,还需要告诉pred节点,其后继现在是newNode。从而实现双向链表的互通,至此,插入操作完成
  7. 最后还是修改当前list的size,并记录该list对象被执行修改的次数。

以上就是指定位置添加的逻辑,先操作新节点,紧接修改原有节点的前驱属性,最后再修改前驱节点的后继属性 

那在这里我们是不是要看下addAll是个什么情况

public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }
public boolean addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index);

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

        Node<E> pred, succ;
        if (index == size) {
            succ = null;
            pred = last;
        } else {
            succ = node(index);
            pred = succ.prev;
        }

        for (Object o : a) {
            @SuppressWarnings("unchecked") E e = (E) o;
            Node<E> newNode = new Node<>(pred, e, null);
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

        if (succ == null) {
            last = pred;
        } else {
            pred.next = succ;
            succ.prev = pred;
        }

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

可以发现,addAll方法比起原来的逐个添加,多的是在添加节点时候对已经添加节点的前驱后继指定。然而这里的操作次数只会记录一次,而不是想象中会根据添加的集合个数来确定

猜你喜欢

转载自blog.csdn.net/Arthurs_L/article/details/81334353
今日推荐