LinkedList

 

正文

LinkedList是一个集合,继承了AbstractList类,实现了List接口,有如下特点:
  1. 双向链表保存元素
  2. 没有指定容量,无需扩容
  3. 使用迭代器进行遍历时,不能修改LinkedList的结构(增删),否则报错
  4. subList方法返回的是class SubList extends AbstractList ,和旧集合共用同一个链表
  5. 线程不安全
  6. 底层数据结构是链表,查询慢,增删快。
  7. 元素可为null

LinkedList_第1张图片

 

 

相关源码解释:

注意:这并不是全部源码,我只是抽了部分我认为关键的

 

public class LinkedList extends AbstractSequentialList implements List, Deque, Cloneable, java.io.Serializable
{
    /** 实际元素个数 */
    transient int size = 0;

    /** 链表头节点 */
    transient Node first;

    /** 链表尾节点 */
    transient Node last;
    /** 链表结构改变次数 */
    protected transient int modCount = 0;

    /**
     * Constructs an empty list.
     */
    public LinkedList() {
    }
    public LinkedList(Collectionextends E> c) {
        this();
        addAll(c);
    }

    /**
     *     将新元素插入链表头
     */
    private void linkFirst(E e) {
        final Node f = first;
        final Node newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        /** 增删都会修改他 */
        modCount++;
    }

    /**
     *     将新元素插入链表尾
     */
    void linkLast(E e) {
        final Node l = last;
        final Node newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }
    
    private E unlinkFirst(Node f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node 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;
    }

    /**
     * Unlinks non-null last node l.
     */
    private E unlinkLast(Node l) {
        // assert l == last && l != null;
        final E element = l.item;
        final Node 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;
    }
    /**
     *     删除指定节点
     * */
    E unlink(Node x) {
        // assert x != null;
        final E element = x.item;
        final Node next = x.next;
        final Node 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;
    }

    /**
     *     获取链表头节点
     */
    public E getFirst() {
        final Node f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

    /**
     *     获取链表尾节点
     */
    public E getLast() {
        final Node l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }

    /**
     *     移除链表头节点
     */
    public E removeFirst() {
        final Node f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

    /**
     *     移除链表尾节点
     */
    public E removeLast() {
        final Node l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }

    /**
     *     添加到链表头
     */
    public void addFirst(E e) {
        linkFirst(e);
    }

    /**
     *     添加到链表尾
     */
    public void addLast(E e) {
        linkLast(e);
    }

    /**
     *     是否包含元素,注意,这是用equals来判断是否相等的
     */
    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }
    /**
     *     获取元素数量
     * */
    public int size() {
        return size;
    }
    /**
     *     添加元素,默认添加到链表尾
     * */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    /**
     *     移除某个元素,注意,这是用equals来判断是否相等的
     */
    public boolean remove(Object o) {
        if (o == null) {
            for (Node x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

   
    /**
     *  清空链表
     */
    public void clear() {
        /*
         *    每个元素都会把头尾节点指针都置为null,避免出现空岛元素,GC失败     
         **/
        for (Node x = first; x != null; ) {
            Node next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }


    // Positional Access Operations

    /**
     *  链表不支持下标获取,所以只能遍历,然后获取
     */
    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }

    /**
     *  设置指定下标的元素
     */
    public E set(int index, E element) {
        checkElementIndex(index);
        Node x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

    /**
     *  在指定位置添加元素
     */
    public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

    /**
     *  删除指定位置元素
     */
    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }



    /**
     *  获取指定下标的节点
     */
    Node node(int index) {
        // assert isElementIndex(index);
        if (index < (size >> 1)) {
            Node x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

    /**
     *  获取元素下标,从头开始找,-1表示找不到
     */
    public int indexOf(Object o) {
        int index = 0;
        if (o == null) {
            for (Node x = first; x != null; x = x.next) {
                if (x.item == null)
                    return index;
                index++;
            }
        } else {
            for (Node x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }

    /**
     *  获取元素下标,从尾开始找,-1表示找不到
     */
    public int lastIndexOf(Object o) {
        int index = size;
        if (o == null) {
            for (Node x = last; x != null; x = x.prev) {
                index--;
                if (x.item == null)
                    return index;
            }
        } else {
            for (Node x = last; x != null; x = x.prev) {
                index--;
                if (o.equals(x.item))
                    return index;
            }
        }
        return -1;
    }


    /**
     *  获取链表头节点,不删除的
     */
    public E peek() {
        final Node f = first;
        return (f == null) ? null : f.item;
    }

    /**
     *  获取链表头节点,不删除的,没有就报错
     */
    public E element() {
        return getFirst();
    }

    /**
     *  链表头节点出栈,会删除的
     */
    public E poll() {
        final Node f = first;
        return (f == null) ? null : unlinkFirst(f);
    }

    /**
     * Retrieves and removes the head (first element) of this list.
     *
     * @return the head of this list
     * @throws NoSuchElementException if this list is empty
     * @since 1.5
     */
    public E remove() {
        return removeFirst();
    }

  

    /**
     * Retrieves, but does not remove, the first element of this list,
     * or returns {@code null} if this list is empty.
     *
     * @return the first element of this list, or {@code null}
     *         if this list is empty
     * @since 1.6
     */
    public E peekFirst() {
        final Node f = first;
        return (f == null) ? null : f.item;
     }

    /**
     * Retrieves, but does not remove, the last element of this list,
     * or returns {@code null} if this list is empty.
     *
     * @return the last element of this list, or {@code null}
     *         if this list is empty
     * @since 1.6
     */
    public E peekLast() {
        final Node l = last;
        return (l == null) ? null : l.item;
    }


    /**
     *  入栈
     */
    public void push(E e) {
        addFirst(e);
    }

    /**
     *  出栈
     */
    public E pop() {
        return removeFirst();
    }

    /**
     * Removes the first occurrence of the specified element in this
     * list (when traversing the list from head to tail).  If the list
     * does not contain the element, it is unchanged.
     *
     * @param o element to be removed from this list, if present
     * @return {@code true} if the list contained the specified element
     * @since 1.6
     */
    public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }



    private static class Node {
        E item;
        Node next;
        Node prev;

        Node(Node prev, E element, Node next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

    
    /**
     *  这个克隆出来的新集合,元素和原来的一模一样,内存都是同一块
     */
    public Object clone() {
        LinkedList 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 x = first; x != null; x = x.next)
            clone.add(x.item);
        return clone;
    }

    /**
     *  简单粗暴,直接创建数组扔进去
     */
    public Object[] toArray() {
        Object[] result = new Object[size];
        int i = 0;
        for (Node x = first; x != null; x = x.next)
            result[i++] = x.item;
        return result;
    }
}

 

你可能感兴趣的:(LinkedList)