【Java集合源码剖析】LinkedList源码剖析

对比了一下jdk1.6和jdk1.8的源码,差别有点太大了,所以决定以大神的jdk1.6分析的为主,先把基础的学会,再弄新的

参考的文章来源:http://blog.csdn.net/ns_code/article/details/35787253


LinkedList简介

    LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈、队列和双端队列来使用。

    LinkedList同样是非线程安全的,只在单线程下适合使用。

    LinkedList实现了Serializable接口,因此它支持序列化,能够通过序列化传输,实现了Cloneable接口,能被克隆。

LinkedList源码剖析

    LinkedList的源码如下(加入了比较详细的注释):

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package java.util;    
  2.    
  3. public class LinkedList<E>    
  4.     extends AbstractSequentialList<E>    
  5.     implements List<E>, Deque<E>, Cloneable, java.io.Serializable    
  6. {      
  7.     // 链表的表头,表头不包含任何数据。Entry是个链表类数据结构。具体实现见最下面
  8.     // 注意:表头是空的,不包含任何数据    
  9.     private transient Entry<E> header = new Entry<E>(nullnullnull);    
  10.    
  11.     // LinkedList中元素个数    
  12.     private transient int size = 0;    
  13.    
  14.     // 默认构造函数:创建一个空的链表 ,把它的next和previous都指向表头   
  15.     public LinkedList() {    
  16.         header.next = header.previous = header;    
  17.     }    
  18.    
  19.     // 包含“集合”的构造函数:创建一个包含“集合”的LinkedList    
  20.     public LinkedList(Collection<? extends E> c) {    
  21.         this();    
  22.         addAll(c);    
  23.     }    
  24.    
  25.     // 获取LinkedList的第一个元素    
  26.     public E getFirst() {    
  27.         if (size==0)    
  28.             throw new NoSuchElementException();    
  29.    
  30.         // 链表的表头header中不包含数据。    
  31.         // 这里返回header所指下一个节点所包含的数据。    
  32.         return header.next.element;    
  33.     }    
  34.    
  35.     // 获取LinkedList的最后一个节点的数据    
  36.     public E getLast()  {    
  37.         if (size==0)    
  38.             throw new NoSuchElementException();    
  39.    
  40.         // 由于LinkedList是双向链表;而表头header不包含数据。    
  41.         // 因而,这里返回表头header的前一个节点所包含的数据。    
  42.         return header.previous.element;    
  43.     }    
  44.    
  45.     // 删除LinkedList的第一个节点,所以不用header.next.element    
  46.     public E removeFirst() {    
  47.         return remove(header.next);    
  48.     }    
  49.    
  50.     // 删除LinkedList的最后一个节点,所以不用header.previous.element    
  51.     public E removeLast() {    
  52.         return remove(header.previous);    
  53.     }    
  54.    
  55.     // 将元素添加到LinkedList的起始位置    
  56.     public void addFirst(E e) {    
  57.         addBefore(e, header.next);    
  58.     }    
  59.    
  60.     // 将元素添加到LinkedList的结束位置
  61.     // 由于是双向链表,所以header的前节点就是LinkList的尾节点    
  62.     public void addLast(E e) {    
  63.         addBefore(e, header);    
  64.     }    
  65.    
  66.     // 判断LinkedList是否包含元素(o) ,其实就是调用indexOf,如果有就返回索引,没有直接返回-1,   
  67.     public boolean contains(Object o) {    
  68.         return indexOf(o) != -1;    
  69.     }    
  70.    
  71.     // 返回LinkedList的大小    
  72.     public int size() {    
  73.         return size;    
  74.     }    
  75.    
  76.     // 将元素(E)添加到LinkedList中,做法上和addLast相同,然而返回值不同    
  77.     public boolean add(E e) {    
  78.         // 将节点(节点数据是e)添加到表头(header)之前。    
  79.         // 即,将节点添加到双向链表的末端。    
  80.         addBefore(e, header);    
  81.         return true;    
  82.     }    
  83.    
  84.     // 从LinkedList中删除元素(o)    
  85.     // 从链表开始查找,如存在元素(o)则删除该元素并返回true;    
  86.     // 否则,返回false。
  87.     // 下面的remove方法里的remove方法调用的是自己的private remove方法
  88.    // private E remove(Entry<E> e){
  89.           if(e == header){
  90.                throw new NoSuchElementException();
  91.           }
  92.           //保留一下要删除的节点
  93.           E result = e.element;
  94.           // 把要删的下一个节点与前一个节点连上,双向连上
  95.           e.previous.next = e.next;
  96.           e.next.previous = e.prevous;
  97.           e.previous = e.next = null;
  98.           e.elment = null;
  99.           size--;   modeCount++;
  100.           return result;
  101.      }  
  102.     public boolean remove(Object o) {   
  103.         if (o==null) {    
  104.             // 若o为null的删除情况
  105.             // 结束的标志是又从表头开始,又回到了表头,双向的链表    
  106.             for (Entry<E> e = header.next; e != header; e = e.next) {    
  107.                 if (e.element==null) {    
  108.                     remove(e);    
  109.                     return true;    
  110.                 }    
  111.             }    
  112.         } else {    
  113.             // 若o不为null的删除情况    
  114.             for (Entry<E> e = header.next; e != header; e = e.next) {    
  115.                 if (o.equals(e.element)) {    
  116.                     remove(e);    
  117.                     return true;    
  118.                 }    
  119.             }    
  120.         }    
  121.         return false;    
  122.     }
  123.    
  124.     // 将“集合(c)”添加到LinkedList中。    
  125.     // 实际上,是从双向链表的末尾开始,将“集合(c)”添加到双向链表中。    
  126.     public boolean addAll(Collection<? extends E> c) {    
  127.         return addAll(size, c);    
  128.     }    
  129.    
  130.     // 从双向链表的index开始,将“集合(c)”添加到双向链表中。    
  131.     public boolean addAll(int index, Collection<? extends E> c) {    
  132.         if (index < 0 || index > size)    
  133.             throw new IndexOutOfBoundsException("Index: "+index+    
  134.                                                 ", Size: "+size);    
  135.         Object[] a = c.toArray();    
  136.         // 获取集合的长度    
  137.         int numNew = a.length;    
  138.         if (numNew==0)    
  139.             return false;    
  140.         modCount++;    
  141.    
  142.         // 设置“当前要插入节点的后一个节点”,比如要插入的位置为index,successor相当于index+1位置
  143.         //private Entry<E> entry(int index),返回当前index坐标的节点,具体实现就是先创建一个
  144.         //header的引用,然后for(header.next())直到循环到index为止,然后return e ,具体实现见下面  
  145.         Entry<E> successor = (index==size ? header : entry(index));    
  146.         // 设置“当前要插入节点的前一个节点”,predecessor相当于index-1的位置    
  147.         Entry<E> predecessor = successor.previous;    
  148.         // 将集合(c)全部插入双向链表中    
  149.         for (int i=0; i<numNew; i++) {
  150.         // 结合Entry的构造函数,这句相当于把a[i]插入successor和predecessor中间 
  151.         // Entry(A,B,C)的效果    C<-A->B,A的下一个连上了B,A的上一个连上了C   
  152.             Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);
  153.        //  插入节点后,将前一节点的next指向当前节点    C->A  使C的next连上了A,最后形成C <=>A->B
  154.             predecessor.next = e;
  155.        // 更新predecessor的指针,这样所有数据从尾部开始插,先插入的一点点向前移动,successor不动
  156.             predecessor = e;    
  157.         }    
          // 将index+1的节点与collection的最后节点相连 
  158.         successor.previous = predecessor;    
  159.    
  160.         // 调整LinkedList的实际大小    
  161.         size += numNew;    
  162.         return true;    
  163.     }    

  164.     // 清空双向链表    
  165.     public void clear() {    
  166.         Entry<E> e = header.next;    
  167.         // 从表头开始,逐个向后遍历;对遍历到的节点执行一下操作:    
  168.         // (01) 设置前一个节点为null     
  169.         // (02) 设置当前节点的内容为null     
  170.         // (03) 设置后一个节点为“新的当前节点”    
  171.         while (e != header) {    
  172.             Entry<E> next = e.next;    
  173.             e.next = e.previous = null;    
  174.             e.element = null;    
  175.             e = next;    
  176.         }    
  177.         header.next = header.previous = header;    
  178.         // 设置大小为0    
  179.         size = 0;    
  180.         modCount++;    
  181.     }    
  182.   }
  183.     // 返回LinkedList指定位置的元素    
  184.     public E get(int index) {    
  185.         return entry(index).element;    
  186.     }    
  187.    
  188.     // 设置index位置对应的节点的值为element    
  189.     public E set(int index, E element) {    
  190.         Entry<E> e = entry(index);    
  191.         E oldVal = e.element;    
  192.         e.element = element;    
  193.         return oldVal;    
  194.     }    
  195.      
  196.     // 在index前添加节点,且节点的值为element    
  197.     public void add(int index, E element) {    
  198.         addBefore(element, (index==size ? header : entry(index)));    
  199.     }    
  200.    
  201.     // 删除index位置的节点    
  202.     public E remove(int index) {    
  203.         return remove(entry(index));    
  204.     }    
  205.    
  206.     // 获取双向链表中指定位置的节点    
  207.     private Entry<E> entry(int index) {    
  208.         if (index < 0 || index >= size)    
  209.             throw new IndexOutOfBoundsException("Index: "+index+    
  210.                                                 ", Size: "+size);    
  211.         Entry<E> e = header;    
  212.         // 获取index处的节点。    
  213.         // 若index < 双向链表长度的1/2,则从前先后查找;    
  214.         // 否则,从后向前查找。    
  215.         if (index < (size >> 1)) {    
  216.             for (int i = 0; i <= index; i++)    
  217.                 e = e.next;    
  218.         } else {    
  219.             for (int i = size; i > index; i--)    
  220.                 e = e.previous;    
  221.         }    
  222.         return e;    
  223.     }    
  224.    
  225.     // 从前向后查找,返回“值为对象(o)的节点对应的索引”    
  226.     // 不存在就返回-1    
  227.     public int indexOf(Object o) {    
  228.         int index = 0;    
  229.         if (o==null) {    
  230.             for (Entry e = header.next; e != header; e = e.next) {    
  231.                 if (e.element==null)    
  232.                     return index;    
  233.                 index++;    
  234.             }    
  235.         } else {    
  236.             for (Entry e = header.next; e != header; e = e.next) {    
  237.                 if (o.equals(e.element))    
  238.                     return index;    
  239.                 index++;    
  240.             }    
  241.         }    
  242.         return -1;    
  243.     }    
  244.    
  245.     // 从后向前查找,返回“值为对象(o)的节点对应的索引”    
  246.     // 不存在就返回-1    
  247.     public int lastIndexOf(Object o) {    
  248.         int index = size;    
  249.         if (o==null) {    
  250.             for (Entry e = header.previous; e != header; e = e.previous) {    
  251.                 index--;    
  252.                 if (e.element==null)    
  253.                     return index;    
  254.             }    
  255.         } else {    
  256.             for (Entry e = header.previous; e != header; e = e.previous) {    
  257.                 index--;    
  258.                 if (o.equals(e.element))    
  259.                     return index;    
  260.             }    
  261.         }    
  262.         return -1;    
  263.     }    
  264.    
  265.     // 返回第一个节点    
  266.     // 若LinkedList的大小为0,则返回null    
  267.     public E peek() {    
  268.         if (size==0)    
  269.             return null;    
  270.         return getFirst();    
  271.     }    
  272.    
  273.     // 返回第一个节点    
  274.     // 若LinkedList的大小为0,则抛出异常    
  275.     public E element() {    
  276.         return getFirst();    
  277.     }    
  278.    
  279.     // 删除并返回第一个节点    
  280.     // 若LinkedList的大小为0,则返回null    
  281.     public E poll() {    
  282.         if (size==0)    
  283.             return null;    
  284.         return removeFirst();    
  285.     }    
  286.    
  287.     // 将e添加双向链表末尾    
  288.     public boolean offer(E e) {    
  289.         return add(e);    
  290.     }    
  291.    
  292.     // 将e添加双向链表开头    
  293.     public boolean offerFirst(E e) {    
  294.         addFirst(e);    
  295.         return true;    
  296.     }    
  297.    
  298.     // 将e添加双向链表末尾    
  299.     public boolean offerLast(E e) {    
  300.         addLast(e);    
  301.         return true;    
  302.     }    
  303.    
  304.     // 返回第一个节点    
  305.     // 若LinkedList的大小为0,则返回null    
  306.     public E peekFirst() {    
  307.         if (size==0)    
  308.             return null;    
  309.         return getFirst();    
  310.     }    
  311.    
  312.     // 返回最后一个节点    
  313.     // 若LinkedList的大小为0,则返回null    
  314.     public E peekLast() {    
  315.         if (size==0)    
  316.             return null;    
  317.         return getLast();    
  318.     }    
  319.    
  320.     // 删除并返回第一个节点    
  321.     // 若LinkedList的大小为0,则返回null    
  322.     public E pollFirst() {    
  323.         if (size==0)    
  324.             return null;    
  325.         return removeFirst();    
  326.     }    
  327.    
  328.     // 删除并返回最后一个节点    
  329.     // 若LinkedList的大小为0,则返回null    
  330.     public E pollLast() {    
  331.         if (size==0)    
  332.             return null;    
  333.         return removeLast();    
  334.     }    
  335.    
  336.     // 将e插入到双向链表开头    
  337.     public void push(E e) {    
  338.         addFirst(e);    
  339.     }    
  340.    
  341.     // 删除并返回第一个节点    
  342.     public E pop() {    
  343.         return removeFirst();    
  344.     }    
  345.    
  346.     // 从LinkedList开始向后查找,删除第一个值为元素(o)的节点    
  347.     // 从链表开始查找,如存在节点的值为元素(o)的节点,则删除该节点    
  348.     public boolean removeFirstOccurrence(Object o) {    
  349.         return remove(o);    
  350.     }    
  351.    
  352.     // 从LinkedList末尾向前查找,删除第一个值为元素(o)的节点    
  353.     // 从链表开始查找,如存在节点的值为元素(o)的节点,则删除该节点    
  354.     public boolean removeLastOccurrence(Object o) {    
  355.         if (o==null) {    
  356.             for (Entry<E> e = header.previous; e != header; e = e.previous) {    
  357.                 if (e.element==null) {    
  358.                     remove(e);    
  359.                     return true;    
  360.                 }    
  361.             }    
  362.         } else {    
  363.             for (Entry<E> e = header.previous; e != header; e = e.previous) {    
  364.                 if (o.equals(e.element)) {    
  365.                     remove(e);    
  366.                     return true;    
  367.                 }    
  368.             }    
  369.         }    
  370.         return false;    
  371.     }    
  372.    
  373.     // 返回“index到末尾的全部节点”对应的ListIterator对象(List迭代器)    
  374.     public ListIterator<E> listIterator(int index) {    
  375.         return new ListItr(index);    
  376.     }    
  377.    
  378.     // List迭代器    
  379.     private class ListItr implements ListIterator<E> {    
  380.         // 上一次返回的节点    
  381.         private Entry<E> lastReturned = header;    
  382.         // 下一个节点    
  383.         private Entry<E> next;    
  384.         // 下一个节点对应的索引值    
  385.         private int nextIndex;    
  386.         // 期望的改变计数。用来实现fail-fast机制。    
  387.         private int expectedModCount = modCount;    
  388.    
  389.         // 构造函数。    
  390.         // 从index位置开始进行迭代    
  391.         ListItr(int index) {    
  392.             // index的有效性处理    
  393.             if (index < 0 || index > size)    
  394.                 throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size);    
  395.             // 若 “index 小于 ‘双向链表长度的一半’”,则从第一个元素开始往后查找;    
  396.             // 否则,从最后一个元素往前查找。    
  397.             if (index < (size >> 1)) {    
  398.                 next = header.next;    
  399.                 for (nextIndex=0; nextIndex<index; nextIndex++)    
  400.                     next = next.next;    
  401.             } else {    
  402.                 next = header;    
  403.                 for (nextIndex=size; nextIndex>index; nextIndex--)    
  404.                     next = next.previous;    
  405.             }    
  406.         }    
  407.    
  408.         // 是否存在下一个元素    
  409.         public boolean hasNext() {    
  410.             // 通过元素索引是否等于“双向链表大小”来判断是否达到最后。    
  411.             return nextIndex != size;    
  412.         }    
  413.    
  414.         // 获取下一个元素    
  415.         public E next() {    
  416.             checkForComodification(); // 检查是否修改了  
  417.             if (nextIndex == size)    
  418.                 throw new NoSuchElementException();    
  419.    
  420.             lastReturned = next;    
  421.             // next指向链表的下一个元素    
  422.             next = next.next;    
  423.             nextIndex++;    
  424.             return lastReturned.element;    
  425.         }    
  426.    
  427.         // 是否存在上一个元素    
  428.         public boolean hasPrevious() {    
  429.             // 通过元素索引是否等于0,来判断是否达到开头。    
  430.             return nextIndex != 0;    
  431.         }    
  432.    
  433.         // 获取上一个元素    
  434.         public E previous() {    
  435.             if (nextIndex == 0)    
  436.             throw new NoSuchElementException();    
  437.    
  438.             // next指向链表的上一个元素    
  439.             lastReturned = next = next.previous;    
  440.             nextIndex--;    
  441.             checkForComodification();    
  442.             return lastReturned.element;    
  443.         }    
  444.    
  445.         // 获取下一个元素的索引    
  446.         public int nextIndex() {    
  447.             return nextIndex;    
  448.         }    
  449.    
  450.         // 获取上一个元素的索引    
  451.         public int previousIndex() {    
  452.             return nextIndex-1;    
  453.         }    
  454.    
  455.         // 删除当前元素。    
  456.         // 删除双向链表中的当前节点    
  457.         public void remove() {    
  458.             checkForComodification();    
  459.             Entry<E> lastNext = lastReturned.next;    
  460.             try {    
  461.                 LinkedList.this.remove(lastReturned);    
  462.             } catch (NoSuchElementException e) {    
  463.                 throw new IllegalStateException();    
  464.             }    
  465.             if (next==lastReturned)    
  466.                 next = lastNext;    
  467.             else   
  468.                 nextIndex--;    
  469.             lastReturned = header;    
  470.             expectedModCount++;    
  471.         }    
  472.    
  473.         // 设置当前节点为e    
  474.         public void set(E e) {    
  475.             if (lastReturned == header)    
  476.                 throw new IllegalStateException();    
  477.             checkForComodification();    
  478.             lastReturned.element = e;    
  479.         }    
  480.    
  481.         // 将e添加到当前节点的前面    
  482.         public void add(E e) {    
  483.             checkForComodification();    
  484.             lastReturned = header;    
  485.             addBefore(e, next);    
  486.             nextIndex++;    
  487.             expectedModCount++;    
  488.         }    
  489.    
  490.         // 判断 “modCount和expectedModCount是否相等”,依次来实现fail-fast机制。    
  491.         final void checkForComodification() {    
  492.             if (modCount != expectedModCount)    
  493.             throw new ConcurrentModificationException();    
  494.         }    
  495.     }    
  496.    
  497.     // 双向链表的节点所对应的数据结构。    
  498.     // 包含3部分:上一节点,下一节点,当前节点值。    
  499.     private static class Entry<E> {    
  500.         // 当前节点所包含的值    
  501.         E element;    
  502.         // 下一个节点    
  503.         Entry<E> next;    
  504.         // 上一个节点    
  505.         Entry<E> previous;    
  506.    
  507.         /**   
  508.          * 链表节点的构造函数。   
  509.          * 参数说明:   
  510.          *   element  —— 节点所包含的数据   
  511.          *   next      —— 下一个节点   
  512.          *   previous —— 上一个节点   
  513.          */   
  514.         Entry(E element, Entry<E> next, Entry<E> previous) {    
  515.             this.element = element;    
  516.             this.next = next;    
  517.             this.previous = previous;    
  518.         }    
  519.     }    
  520.    
  521.     // 将节点(节点数据是e)添加到entry节点之前。    
  522.     private Entry<E> addBefore(E e, Entry<E> entry) {    
  523.         // 新建节点newEntry,将newEntry插入到节点e之前;并且设置newEntry的数据是e    
  524.         Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);    
  525.         newEntry.previous.next = newEntry;    
  526.         newEntry.next.previous = newEntry;    
  527.         // 修改LinkedList大小    
  528.         size++;    
  529.         // 修改LinkedList的修改统计数:用来实现fail-fast机制。    
  530.         modCount++;    
  531.         return newEntry;    
  532.     }    
  533.    
  534.     // 将节点从链表中删除    
  535.     private E remove(Entry<E> e) {    
  536.         if (e == header)    
  537.             throw new NoSuchElementException();    
  538.    
  539.         E result = e.element;    
  540.         e.previous.next = e.next;    
  541.         e.next.previous = e.previous;    
  542.         e.next = e.previous = null;    
  543.         e.element = null;    
  544.         size--;    
  545.         modCount++;    
  546.         return result;    
  547.     }    
  548.    
  549.     // 反向迭代器    
  550.     public Iterator<E> descendingIterator() {    
  551.         return new DescendingIterator();    
  552.     }    
  553.    
  554.     // 反向迭代器实现类。    
  555.     private class DescendingIterator implements Iterator {    
  556.         final ListItr itr = new ListItr(size());    
  557.         // 反向迭代器是否下一个元素。    
  558.         // 实际上是判断双向链表的当前节点是否达到开头    
  559.         public boolean hasNext() {    
  560.             return itr.hasPrevious();    
  561.         }    
  562.         // 反向迭代器获取下一个元素。    
  563.         // 实际上是获取双向链表的前一个节点    
  564.         public E next() {    
  565.             return itr.previous();    
  566.         }    
  567.         // 删除当前节点    
  568.         public void remove() {    
  569.             itr.remove();    
  570.         }    
  571.     }    
  572.    
  573.    
  574.     // 返回LinkedList的Object[]数组    
  575.     public Object[] toArray() {    
  576.     // 新建Object[]数组    
  577.     Object[] result = new Object[size];    
  578.         int i = 0;    
  579.         // 将链表中所有节点的数据都添加到Object[]数组中    
  580.         for (Entry<E> e = header.next; e != header; e = e.next)    
  581.             result[i++] = e.element;    
  582.     return result;    
  583.     }    
  584.    
  585.     // 返回LinkedList的模板数组。所谓模板数组,即可以将T设为任意的数据类型    
  586.     public <T> T[] toArray(T[] a) {    
  587.         // 若数组a的大小 < LinkedList的元素个数(意味着数组a不能容纳LinkedList中全部元素)    
  588.         // 则新建一个T[]数组,T[]的大小为LinkedList大小,并将该T[]赋值给a。    
  589.         if (a.length < size)    
  590.             a = (T[])java.lang.reflect.Array.newInstance(    
  591.                                 a.getClass().getComponentType(), size);    
  592.         // 将链表中所有节点的数据都添加到数组a中    
  593.         int i = 0;    
  594.         Object[] result = a;    
  595.         for (Entry<E> e = header.next; e != header; e = e.next)    
  596.             result[i++] = e.element;    
  597.    
  598.         if (a.length > size)    
  599.             a[size] = null;    
  600.    
  601.         return a;    
  602.     }    
  603.    
  604.    
  605.     // 克隆函数。返回LinkedList的克隆对象。    
  606.     public Object clone() {    
  607.         LinkedList<E> clone = null;    
  608.         // 克隆一个LinkedList克隆对象    
  609.         try {    
  610.             clone = (LinkedList<E>) super.clone();    
  611.         } catch (CloneNotSupportedException e) {    
  612.             throw new InternalError();    
  613.         }    
  614.    
  615.         // 新建LinkedList表头节点    
  616.         clone.header = new Entry<E>(nullnullnull);    
  617.         clone.header.next = clone.header.previous = clone.header;    
  618.         clone.size = 0;    
  619.         clone.modCount = 0;    
  620.    
  621.         // 将链表中所有节点的数据都添加到克隆对象中    
  622.         for (Entry<E> e = header.next; e != header; e = e.next)    
  623.             clone.add(e.element);    
  624.    
  625.         return clone;    
  626.     }    
  627.    
  628.     // java.io.Serializable的写入函数    
  629.     // 将LinkedList的“容量,所有的元素值”都写入到输出流中    
  630.     private void writeObject(java.io.ObjectOutputStream s)    
  631.         throws java.io.IOException {    
  632.         // Write out any hidden serialization magic    
  633.         s.defaultWriteObject();    
  634.    
  635.         // 写入“容量”    
  636.         s.writeInt(size);    
  637.    
  638.         // 将链表中所有节点的数据都写入到输出流中    
  639.         for (Entry e = header.next; e != header; e = e.next)    
  640.             s.writeObject(e.element);    
  641.     }    
  642.    
  643.     // java.io.Serializable的读取函数:根据写入方式反向读出    
  644.     // 先将LinkedList的“容量”读出,然后将“所有的元素值”读出    
  645.     private void readObject(java.io.ObjectInputStream s)    
  646.         throws java.io.IOException, ClassNotFoundException {    
  647.         // Read in any hidden serialization magic    
  648.         s.defaultReadObject();    
  649.    
  650.         // 从输入流中读取“容量”    
  651.         int size = s.readInt();    
  652.    
  653.         // 新建链表表头节点    
  654.         header = new Entry<E>(nullnullnull);    
  655.         header.next = header.previous = header;    
  656.    
  657.         // 从输入流中将“所有的元素值”并逐个添加到链表中    
  658.         for (int i=0; i<size; i++)    
  659.             addBefore((E)s.readObject(), header);    
  660.     }    
  661.    


几点总结

    关于LinkedList的源码,给出几点比较重要的总结:

    1、从源码中很明显可以看出,LinkedList的实现是基于双向循环链表的,且头结点中不存放数据,如下图;


    2、注意两个不同的构造方法。无参构造方法直接建立一个仅包含head节点的空链表,包含Collection的构造方法,先调用无参构造方法建立一个空链表,而后将Collection中的数据加入到链表的尾部后面。

    3、在查找和删除某元素时,源码中都划分为该元素为null和不为null两种情况来处理,LinkedList中允许元素为null。

    4、LinkedList是基于链表实现的,因此不存在容量不足的问题,所以这里没有扩容的方法。

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

    6、注意链表类对应的数据结构Entry。如下;

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 双向链表的节点所对应的数据结构。    
  2. // 包含3部分:上一节点,下一节点,当前节点值。    
  3. private static class Entry<E> {    
  4.     // 当前节点所包含的值    
  5.     E element;    
  6.     // 下一个节点    
  7.     Entry<E> next;    
  8.     // 上一个节点    
  9.     Entry<E> previous;    
  10.   
  11.     /**   
  12.      * 链表节点的构造函数。   
  13.      * 参数说明:   
  14.      *   element  —— 节点所包含的数据   
  15.      *   next      —— 下一个节点   
  16.      *   previous —— 上一个节点   
  17.      */   
  18.     Entry(E element, Entry<E> next, Entry<E> previous) {    
  19.         this.element = element;    
  20.         this.next = next;    
  21.         this.previous = previous;    
  22.     }    
  23. }    
    7、LinkedList是基于链表实现的,因此插入删除效率高,查找效率低(虽然有一个加速动作)
    8、要注意源码中还实现了栈和队列的操作方法,因此也可以作为栈、队列和双端队列来使用。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值