LinkedList类注释翻译、源码分析

一、类注释翻译

 * Doubly -linked list implementation of the {@code List} and {@code Deque}
 * interfaces.  Implements all optional list operations, and permits all
 * elements (including {@code null}).

使用“双向链表”来实现List与Deque接口。 实现了所有List接口中的方法,并且允许存放所有元素,包括Null。

* 

All of the operations perform as could be expected for a doubly- linked * list. Operations that index into the list will traverse the list from * the beginning or the end, whichever is closer to the specified index.

所有的操作都可通过双向链表完成。通过从开头或者结尾遍历集合,去接近要操作的那个元素。

 * 

Note that this implementation is not synchronized. * If multiple threads access a linked list concurrently, and at least * one of the threads modifies the list structurally, it must be * synchronized externally. (A structural modification is any operation * that adds or deletes one or more elements; merely setting the value of * an element is not a structural modification.) This is typically * accomplished by synchronizing on some object that naturally * encapsulates the list. * * If no such object exists, the list should be "wrapped" using the * {@link Collections#synchronizedList Collections.synchronizedList} * method. This is best done at creation time, to prevent accidental * unsynchronized access to the list:

 *   List list = Collections.synchronizedList(new LinkedList(...));
* *

The iterators returned by this class's {@code iterator} and * {@code listIterator} methods are fail- fast : if the list is * structurally modified at any time after the iterator is created, in * any way except through the Iterator's own {@code remove} or * {@code add} methods, the iterator will throw a {@link * ConcurrentModificationException} . Thus, in the face of concurrent * modification, the iterator fails quickly and cleanly, rather than * risking arbitrary, non - deterministic behavior at an undetermined * time in the future. * *

Note that the fail - fast behavior of an iterator cannot be guaranteed * as it is, generally speaking, impossible to make any hard guarantees in the * presence of unsynchronized concurrent modification. Fail- fast iterators * throw {@code ConcurrentModificationException} on a best- effort basis. * Therefore, it would be wrong to write a program that depended on this * exception for its correctness: the fail- fast behavior of iterators * should be used only to detect bugs.

这几段的翻译与ArrayList相似,请参考:http://blog.csdn.net/disiwei1012/article/details/73947173

二、自己动手实现双向链表

1.定义node节点类
2.实现基本方法

package com.demo4;

public class DoubleLinkList {

      int length;
      private Node header;
      private Node tail;

      public DoubleLinkList() {
           length = 0;
           header = null ;
           tail = null ;
     }

      /**
      * 打印链表所有元素
      */
      public void printAll(){
          Node currentNode = header ;
           while (currentNode != null){
              System. out .println(currentNode .obj );
               currentNode = currentNode .next ;
          }
     }

      /**
      * 删除指定位置元素
      * @param index
      */
      public void removeByIndex(Integer index ){
          checkRangIndex( index);
          Node node = this .getNode(index );
           if (node .prev == null){
               header = node. next;
               header. prev = null ;
          } else if (node .next == null){
               tail = node. prev;
               tail. next = null ;
          } else {
              Node preNode = node. prev;
              Node nextNode = node. next;
               preNode. next = nextNode;
               nextNode. prev = preNode;
          }
           length--;
           node = null ;//等待GC回收
     }

      /**
      * 尾插法插入元素
      * @param obj
      */
      public void addTail(Object obj ){
           this .addTailByIndex(obj , null);
     }

      /**
      * 头插法插入元素
      * @param obj
      */
      public void addHeader(Object obj ){
           this .addHeaderByIndex(obj , null);
     }

      /**
      * 尾插法插入元素,指定位置
      * @param obj
      * @param index 指定具体插入位置
      */
      public void addTailByIndex(Object obj ,Integer index ){
           if (index == null){
               if (header == null){
                   createHeader( new Node(null , null, obj));
              } else {
                   Node node = new Node(tail , null, obj);
                    tail. next = node;
                    tail = node;
                    length++;
              }
          } else {
              checkRangIndex( index);
              Node node = this .getNode(index );
               node. next = null ;
               node. prev = tail;
               tail. next = node;
               tail = node;
               length++;
          }
     }

      /**
      * 头插法插入元素,指定位置
      * @param obj
      * @param index 指定具体插入位置
      */
      public void addHeaderByIndex(Object obj ,Integer index ){
           if (index == null){
               if (header == null){
                   createHeader( new Node(null , null, obj));
              } else {
                   Node node = new Node(null ,header ,obj );
                    header. prev = node;
                    header = node;
                    length++;
              }
          } else {
              checkRangIndex( index);
              Node node = this .getNode(index );
               node. next = header;
               node. prev = null ;
               header. prev = node;
               header = node;
               length++;
          }
     }

      /**
      * 根据索引获取节点,如果索引在链表的前半部分,则从头节点开始
      * 如果索引在链表的后半部分,则从尾节点开始
      * @param index
      * @return
      */
      public Node getNode( int index ){
          checkRangIndex( index);
          Node currentNode = null ;
           if (index < length /2){
               currentNode = header ;
               for (int i = 0;i else {
               currentNode = tail ;
               for (int i =length -1;i >index ;i --){
                    currentNode = currentNode .prev ;
              }
          }
           return currentNode ;
     }

      /**
      * 判断索引地址是否合法
      * @param index
      */
      public void checkRangIndex( int index){
           if (index < 0 || index > length-1){
               throw new RuntimeException( "索引地址不合法!" );
          }
     }

      /**
      * 链表无节时创建头结点,header和tail指向统一节点
      * @param node
      */
      public void createHeader(Node node ){
           header = node;
           tail = header;
           length++;
     }

      private static class Node{
          Node prev;
          Node next;
          Object obj;

           public Node(Node prev ,Node next ,Object obj ) {
               this .prev = prev ;
               this .next = next ;
               this .obj = obj ;
          }

     }
}

三、源码分析

1.我们来看下LinkedList是如果定义双向节点的:
和我定义的差别不大。

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

LinkedList继承关系如下:
LinkedList类注释翻译、源码分析_第1张图片

LinkedList继承抽象类AbstractSequentialList,我们知道抽象类可以选择实现接口中的方法,也可以不实现接口中的方法,
LinkedList只需实现AbstractSequentialList中未实现的方法、Deque接口中规定的方法、List接口中规定的方法就好了。

public class LinkedList<E>
    extends AbstractSequentialList
    implements List, Deque, Cloneable, java.io.Serializable
{
    //存储元素个数,即链表节点的个数
    transient int size = 0;

    /**
     * 头节点
     * 满足这两个条件
     * 1.(first == null && last == null) 2.(first.prev == null && first.item != null)
     */
    transient Node first;

    /**
     * 尾节点
     * 满足这两个条件
     * 1. (first == null && last == null) 2.(last.next == null && last.item != null)
     */
    transient Node last;

    /**
     * 空构造函数
     */
    public LinkedList() {
    }

    /**
     * 构造一个LinkedList,并将集合c中的元素全部放入
     *
     * @param   c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    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;
        //元素个数+1
        size++;
        //修改次数+1
        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;
        //元素个数+1
        size++;
        //修改次数+1
        modCount++;
    }

    /**
     * 在节点succ前插入节点,succ不能为null,否则会抛出空指针
     */
    void linkBefore(E e, Node succ) {
        // assert succ != null;
        final Node pred = succ .prev ;
        final Node newNode = new Node<>( pred, e, succ );
        succ. prev = newNode;
        //如果succ无前节点,succ默认为头结点
        if (pred == null)
            first = newNode;
        else
            pred. 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 ; //GC回收
        //首节点的后一个节点为新的首节点
        first = next;
        if (next == null)
            last = null ;
        else
            next. prev = null ;
        //元素个数-1
        size--;
        //修改次数+1
        modCount++;
        //返回被删除元素
        return element ;
    }

    /**
     * 删除尾节点
     */
    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
        //尾节点的prev节点作为新的尾节点
        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 ;
        //x的下一个节点
        final Node next = x .next ;
        //x的前一个节点
        final Node prev = x .prev ;
        //如果无前节点,则x为头节点
        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 ;
    }

    /**
     * 返回头节点中保存的数据,如果头节点为null,则抛出NoSuchElementException
     * @return the first element in this list
     * @throws NoSuchElementException if this list is empty
     */
    public E getFirst() {
        final Node f = first ;
        if (f == null)
            throw new NoSuchElementException();
        return f .item ;
    }

    /**
     * 返回尾节点中保存的数据,如果尾节点为null,则抛出NoSuchElementException
     *
     * @return the last element in this list
     * @throws NoSuchElementException if this list is empty
     */
    public E getLast() {
        final Node l = last ;
        if (l == null)
            throw new NoSuchElementException();
        return l .item ;
    }

    /**
     * 移除头节点,并返回头节点
     * @return the first element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeFirst() {
        final Node f = first ;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f );
    }

    /**
     * 移除尾节点,并返回尾节点
     *
     * @return the last element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeLast() {
        final Node l = last ;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l );
    }

    /**
     * 头插法插入元素
     *
     * @param e the element to add
     */
    public void addFirst(E e ) {
        linkFirst( e);
    }

    /**
     * 尾插法插入元素
     *
     * @param e the element to add
     */
    public void addLast(E e ) {
        linkLast( e);
    }

    /**
     * 判断集合中是否至少有一个节点的数据等于o
     *
     * @param o element whose presence in this list is to be tested
     * @return {@code true} if this list contains the specified element
     */
    public boolean contains(Object o ) {
        return indexOf(o ) != -1;
    }

    /**
     * 返回存储节点的个数
     *
     * @return the number of elements in this list
     */
    public int size() {
        return size ;
    }

    /**
     * 尾插法插入元素
     */
    public boolean add(E e ) {
        linkLast( e);
        return true ;
    }

    /**
     * 移除集合中第一个出现的节点中的数据等于o的节点,如果集合中不包含该元素,则什么都不做
     * 如果o为null,则删除第一个数据为null的节点
     */
    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 ;
    }

    /*
     * 将集合c中的元素全部加到链表的尾部
     */
    public boolean addAll(Collectionextends E> c) {
        return addAll(size , c );
    }

    /**
     * 将集合c插入到双向链表中,插入的位置是index之后
     */
    public boolean addAll(int index , Collectionextends E> c ) {
        checkPositionIndex( index);//检测index是否>=0或者

        Object[] a = c.toArray();
        //新加入节点个数
        int numNew = a .length ;
        if (numNew == 0)//如果没有节点要插入返回false
            return false ;

        //如果index等于size,则相当于插入尾部
        Node 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 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 ;
    }

    /**
     * 移除集合中全部节点,相当于清空集合,为了确保被GC回收,清空引用
     * The list will be empty after this call returns.
     */
    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 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 ));
    }

    /**
     * Tells if the argument is the index of an existing element.
     */
    private boolean isElementIndex( int index) {
        return index >= 0 && index < size;
    }

    /**
     * 判断一个索引是否在链表允许的范围内
     */
    private boolean isPositionIndex( int index) {
        return index >= 0 && index <= size;
    }

    private String outOfBoundsMsg( int index ) {
        return "Index: " +index +", Size: " +size ;
    }

    private void checkElementIndex( int index) {
        if (!isElementIndex(index ))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index ));
    }

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

    /**
     * 返回指定位置的节点,和我写的差不多
     */
    Node node( int index ) {
        // 若index < 双向链表长度的1/2,则从前先后查找;   
        // 否则,从后向前查找。 
        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 ;
        }
    }

    // Search Operations

    /**
     * 查询对象o在集合中的位置
     *  如果对象o为null,返回第一个节点数据为null的节点的位置,
     * 否则,返回第一个一个匹配节点的位置。如果未找到,则返回-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;
    }

    /**
     * 查询对象o在集合中的位置
     * 如果对象o为null,返回最后一个节点数据为null的节点的位置,
     * 否则,返回最后一个匹配节点的位置。如果未找到,则返回-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;
    }

    // Queue operations.实现队列接口中的方法

    /**
     * 返回头节点的数据,如果节点为null,则返回null
     * @return the head of this list, or {@code null} if this list is empty
     * @since 1.5
     */
    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);
    }

    /**
     * 返回头节点,并将头节点删除
     *
     */
    public E remove() {
        return removeFirst();
    }

    /**
     * 在尾部增加一个元素e
     */
    public boolean offer(E e ) {
        return add(e );
    }

    // Deque operations
    /**
     * 在头部增加一个元素e
     */
    public boolean offerFirst(E e ) {
        addFirst( e);
        return true ;
    }

    public boolean offerLast(E e ) {
        addLast( e);
        return true ;
    }

    public E peekFirst() {
        final Node f = first ;
        return (f == null) ? null : f. item;
     }

    public E peekLast() {
        final Node l = last ;
        return (l == null) ? null : l. item;
    }

    public E pollFirst() {
        final Node f = first ;
        return (f == null) ? null : unlinkFirst( f);
    }

    public E pollLast() {
        final Node 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 ) {
        if (o == null) {
            for (Node x = last ; x != null; x = x. prev) {
                if (x .item == null) {
                    unlink( x);
                    return true ;
                }
            }
        } else {
            for (Node x = last ; x != null; x = x. prev) {
                if (o .equals(x .item )) {
                    unlink( x);
                    return true ;
                }
            }
        }
        return false ;
    }

    /**
     * 在抽象类AbstractSequentialList中唯一没有实现的方法
     * 返回该集合ListIterator迭代器
     */
    public ListIterator listIterator( int index ) {
        checkPositionIndex( index);
        return new ListItr( index);
    }

    private class ListItr implements ListIterator<E> {
        private Node lastReturned = null;//最后一个返回的节点,也就是当前持有的节点
        private Node next ;//当前持有节点的下一个节点
        private int nextIndex ;//当前持有节点的下一个节点位置
        private int expectedModCount = modCount ;//实现fast-fail

        ListItr( int index ) {
            next = ( index == size) ? null : node(index );
            nextIndex = index ;
        }

        //根据nextIndex是否等于size,判断时候是否还有下一个节点
        public boolean hasNext() {
            return nextIndex < size ;
        }
         获取下一个元素
        public E next() {
            checkForComodification();
            if (!hasNext())
                throw new NoSuchElementException();

            lastReturned = next ;
            next = next. next;
            nextIndex ++;
            return lastReturned .item ;
        }
         是否有前一个节点
        public boolean hasPrevious() {
            return nextIndex > 0;
        }

        //获取前一个节点
        public E previous() {
            checkForComodification();
            if (!hasPrevious())
                throw new NoSuchElementException();

            lastReturned = next = (next == null) ? last : next .prev ;
            nextIndex --;
            return lastReturned .item ;
        }
        //获取下一个节点位置
        public int nextIndex() {
            return nextIndex ;
        }
        //获取前一个节点位置
        public int previousIndex() {
            return nextIndex - 1;
        }
        //移除当前持有节点
        public void remove() {
            checkForComodification();
            if (lastReturned == null)
                throw new IllegalStateException();

            Node lastNext = lastReturned .next ;
            unlink( lastReturned );
            if (next == lastReturned )
                next = lastNext;
            else
                nextIndex --;
            lastReturned = null ;
            expectedModCount ++;
        }
        //替换当前持有节点的值为e
        public void set(E e ) {
            if (lastReturned == null)
                throw new IllegalStateException();
            checkForComodification();
            lastReturned .item = e ;
        }
        //在当前持有节点后插入节点e
        public void add(E e ) {
            checkForComodification();
            lastReturned = null ;
            if (next == null)
                linkLast( e);
            else
                linkBefore( e, next);
            nextIndex ++;
            expectedModCount ++;
        }

        final void checkForComodification() {//fast-fail具体时间
            if (modCount != expectedModCount )
                throw new ConcurrentModificationException();
        }
    }
    //节点定义
    private static class Node<E> {
        E item;
        Node next;
        Node prev;

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

    /**
     * @since 1.6
     */
    public Iterator descendingIterator() {
        return new DescendingIterator ();
    }

    /**
     * Adapter to provide descending iterators via ListItr.previou
     * 从后遍历集合,通过ListItr
     */
    private class DescendingIterator implements Iterator<E> {
        private final ListItr itr = new ListItr(size());
        public boolean hasNext() {
            return itr .hasPrevious();
        }
        public E next() {
            return itr .previous();
        }
        public void remove() {
            itr.remove();
        }
    }

    @SuppressWarnings( "unchecked" )
    private LinkedList superClone() {
        try {
            return (LinkedList) super.clone();
        } catch (CloneNotSupportedException e ) {
            throw new InternalError();
        }
    }

    /**
      *浅拷贝集合,不拷贝元素,自拷贝引用
     * @return a shallow copy of this {@code LinkedList} instance
     */
    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 ;
    }

    /**
     * 返回一个包含该集合所有元素的集合,为了保证集合数组的安全,需要构建一个新数组
     * 

This method acts as bridge between array -based and collection- based * APIs. * * @return an array containing all of the elements in this list * in proper sequence */ 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 ; } /** * 给一个数组a,返回一个数组(数组元素按照集合从前往后的顺序排列),该数组包含了集合中的所有元素 * 若数组a的长度不足以装入所有的集合元素,则使用Array.newInstance()这一方法创建一个size大小, * 元素类型为数组a的元素类型的数组,并将该数组赋值给a */ @SuppressWarnings( "unchecked" ) public 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 x = first ; x != null; x = x. next) result[ i++] = x. item; if (a .length > size ) a[ size] = null ; return a ; } }

你可能感兴趣的:(深入理解java,java集合类源码分析)