19java源码解析-LinkedList(一)

其他 源码解析 https://blog.csdn.net/qq_32726809/article/category/8035214

通过大体浏览源码,可知,Linkedlist的存储机构是一个链表

类的声明

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
  • AbstractSequentialList
  • 顺序集合接口的最小实现
  • 若是随机插入,则优先用AbstractList
  • Deque
  • 删除和插入的集合,双端对列

节点内部类

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

next 指的是下一个节点,prev指的是上一个节点,item指的是当前节点的元素。可以将next和perv理解为指针。

属性

 

transient int size = 0;
transient Node<E> first;/*第一个节点*/
transient Node<E> last;/*最后一个节点*/

构造函数

 public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

按照集合的迭代器的元素获取构造函数

3方法

3.1linkFirst

把元素e作为第一个元素

 private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

将node节点上一个元素置为null,下一个元素为 原第一个元素,并将现在的元素赋予 first

3.2linkLast

把元素e多为最后一个元素

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

将当前元素新建一个node节点,把尾部的节点作为这个节点的上一个节点,把下一个节点置为null,把这个节点作为上一个节点的下一个节点,总的来说,就是把新节点与原来的节点连接起来.

3.3linkBefore

把元素e插入到 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;
        else
            pred.next = newNode;
        size++;
        modCount++;
    }

类似于在两个火车车厢中间插入新的车厢,需要旧的车厢与新的车厢头尾相连,并改变车厢数

3.4unlinkFirst

将第一个元素移除链接

 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;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }

3.5unlinkLast

将最后一个元素移除处链接

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;
        if (prev == null)
            first = null;
        else
            prev.next = null;
        size--;
        modCount++;
        return element;
    }

3.6getFirst

获取第一个元素

   public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

3.7getLast

获取最后一个元素

 public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }

3.8removeFirst(),removeLast(),addFirst(E e),addLast(E e)

这些方法用的是上面已经讲过的方法

  public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

    /**
     * Removes and returns the last element from this list.
     *
     * @return the last element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }

    /**
     * Inserts the specified element at the beginning of this list.
     *
     * @param e the element to add
     */
    public void addFirst(E e) {
        linkFirst(e);
    }

    /**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #add}.
     *
     * @param e the element to add
     */
    public void addLast(E e) {
        linkLast(e);
    }

3.9contains

判断元素是否在集合中,其实就是判断节点的索引是否大于小于0

 public boolean contains(Object o) {
        return indexOf(o) != -1;
    }
    
 public int indexOf(Object o) {/*--------------1*/
        int index = 0;
        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) {/*--------------2*/
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }
  • 1处方法的是获取元素的所在位置
  • 2处是遍历链条,对每个节点进行判断。

3.10add

 public boolean add(E e) {
        linkLast(e);
        return true;
    }

新添加的元素会被添加到最后

3.11 remove

通过遍历链表移除元素

 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;
    }
    
 E unlink(Node<E> x) {  /*-------------1*/
        // 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;
    }

1处的函数是删除元素,然后将删除后的两端重新链接起来

3.12addAll

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


        /*-----------------------1*/
        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;
    }
   

1处为核心代码,就是把集合转化的数组进行遍历,然后将元素新建成节点,再将节点链接起来

3.13clear

将所有连接元素设置为空

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

猜你喜欢

转载自blog.csdn.net/qq_32726809/article/details/82855504