1、LinkedList-底层使用数组实现

  1. 1LinkedList 是一个继承于AbstractSequentialList的双向链表。
  2. 1-1AbstractSequentialList 实现了get(int index)、set(int index, E element)、add(int index, E element) remove(int index)这些函数。这些接口都是随机访问List的。
  3. 1-2、若需要通过AbstractSequentialList自己实现一个列表,只需要扩展此类,并提供 listIterator() size() 方法的实现即可。若要实现不可修改的列表,则需要实现列表迭代器的 hasNextnexthasPreviousprevious index 方法即可。
  4. 2、存储的数据结构是链表:
  5. public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
  6. //(集合中)双向链表中节点的个数。
  7. transient int size = 0;
  8. /**
  9. * Pointer to first node.
  10. * 头节点 first
  11. * 头节点 first 的 prev = null
  12. * Invariant: (first == null && last == null) ||
  13. * (first.prev == null && first.item != null)
  14. */
  15. transient Node<E> first;
  16. /**
  17. * Pointer to last node.
  18. * 尾节点 last
  19. * 尾节点 last 的 next = null
  20. * Invariant: (first == null && last == null) ||
  21. * (last.next == null && last.item != null)
  22. */
  23. transient Node<E> last;
  24. //节点是一个双向节点,私有的静态内部类
  25. private static class Node<E> {
  26. E item; //数据
  27. Node<E> next; //指向后面的节点
  28. Node<E> prev; //指向前面的节点
  29. Node(Node<E> prev, E element, Node<E> next) {
  30. this.item = element;
  31. this.next = next;
  32. this.prev = prev;
  33. }
  34. }
  35. }

2、LinkedList-构造方法

  1. 1LinkedList提供了两种方式的构造器:
  2. //构造一个空的LinkedList
  3. public LinkedList() {
  4. }
  5. //接收一个Collection参数c,默认构造方法构造一个空的链表,并通过addAll将c中的元素全部添加到链表中。
  6. public LinkedList(Collection<? extends E> c) {
  7. this();
  8. addAll(c);
  9. }
  10. 2、所以我们构建LinkedList实例是可以:
  11. LinkedList<E> list = new LinkedList<E>(); // 普通创建方法
  12. ==>
  13. LinkedList<E> list = new LinkedList(Collection<? extends E> c); // 使用集合创建链表

3、LinkedList-内部方法

  1. 1LinkedList-关键的几个内部方法(头部添加删除,尾部添加删除,获取指定节点,指定节点的添加删除),时间复杂度为O(1)
  2. //节点是一个双向节点,私有的静态内部类
  3. private static class Node<E> {
  4. E item; //数据
  5. Node<E> next; //指向后面的节点
  6. Node<E> prev; //指向前面的节点
  7. Node(Node<E> prev, E element, Node<E> next) {
  8. this.item = element;
  9. this.next = next;
  10. this.prev = prev;
  11. }
  12. }
  13. /**
  14. * Links e as first element.
  15. * e作为第一个元素,即该元素插入到头部[作为头节点]
  16. * 所有的遍历都是从这两个节点开始的
  17. */
  18. private void linkFirst(E e) {
  19. //获取头节点
  20. final Node<E> f = first;
  21. //新建一个节点,新节点的前指针指向null,尾指针指向之前的头元素first[看构造器方法]
  22. final Node<E> newNode = new Node<>(null, e, f);
  23. first = newNode; //头节点的next指针指向新建的节点
  24. //如果头节点是空的,则说明是空列表,新建的节点作为头节点,则将last节点的next修改指向新建的节点
  25. if (f == null)
  26. last = newNode;
  27. else
  28. f.prev = newNode;//原来的头节点的前指针指向新建的节点
  29. size++;
  30. modCount++;
  31. }
  32. /**
  33. * Links e as last element.
  34. * e作为最后一个元素,即该元素插入到尾部[作为尾节点]
  35. * 所有的遍历都是从这两个节点开始的
  36. */
  37. void linkLast(E e) {
  38. //获取尾节点
  39. final Node<E> l = last;
  40. //新建一个节点,新节点的尾指针指向null,前指针指向之前的尾元素last[看构造器方法]
  41. final Node<E> newNode = new Node<>(l, e, null);
  42. //将尾节点的next指针指向新建的节点
  43. last = newNode;
  44. if (l == null)
  45. first = newNode; //为空时就说明此链表中没有数据,头结点也变更为新增的节点
  46. else
  47. l.next = newNode; //原来的尾节点变为倒数第2个,原理尾节点后驱指向新增的节点
  48. size++;
  49. modCount++;
  50. }
  51. /**
  52. * Inserts element e before non-null Node succ.
  53. * 在 非空的指定节点 前面插入一个元素,这里假设 指定节点不为 null。
  54. * @param e 需要插入的元素
  55. * @param succ 指定的节点
  56. */
  57. void linkBefore(E e, Node<E> succ) {
  58. // assert succ != null;
  59. // 获取指定节点 succ 前面的一个节点
  60. final Node<E> pred = succ.prev;
  61. //新建一个节点,头部指向 succ 前面的节点,尾部指向 succ 节点,数据为 e
  62. final Node<E> newNode = new Node<>(pred, e, succ);
  63. //让 succ 节点头部指向 新建的节点
  64. succ.prev = newNode;
  65. if (pred == null)
  66. first = newNode;//如果 succ 前面的节点为空,说明 succ 就是第一个节点,那现在新建的节点就变成第一个节点了
  67. else
  68. pred.next = newNode;//如果前面有节点,让前面的节点后指针指向新建节点
  69. size++;
  70. modCount++;
  71. }
  72. /**
  73. * Unlinks non-null first node f.
  74. * 删除头节点(Node<E> f)并返回该节点上的数据,假设不为 null
  75. */
  76. private E unlinkFirst(Node<E> f) {
  77. // assert f == first && f != null;
  78. //获取数据,一会儿返回,假设不为 null。
  79. final E element = f.item;
  80. //获取头节点后面的一个节点
  81. final Node<E> next = f.next;
  82. //使头节点上数据为空,尾部指向空
  83. f.item = null;
  84. f.next = null; // help GC
  85. //现在头节点后边的节点变成第一个了
  86. first = next;
  87. //如果头节点后面的节点为 null,说明移除这个节点后,链表里没节点了
  88. if (next == null)
  89. last = null;
  90. else
  91. next.prev = null;
  92. size--;
  93. modCount++;
  94. return element;
  95. }
  96. /**
  97. * Unlinks non-null last node l.
  98. * 删除尾部节点(Node<E> l)并返回,假设不为空
  99. */
  100. private E unlinkLast(Node<E> l) {
  101. // assert l == last && l != null;
  102. //获取数据,一会儿返回,假设不为 null。
  103. final E element = l.item;
  104. //获取倒数第二个节点
  105. final Node<E> prev = l.prev;
  106. //尾节点数据、尾指针置为空
  107. l.item = null;
  108. l.prev = null; // help GC
  109. //现在倒数第二变成倒数第一了
  110. last = prev;
  111. if (prev == null)
  112. first = null;
  113. else
  114. prev.next = null;
  115. size--;
  116. modCount++;
  117. return element;
  118. }
  119. /**
  120. * Unlinks non-null node x.
  121. * 删除某个指定节点
  122. */
  123. E unlink(Node<E> x) {
  124. // assert x != null;
  125. //获取数据,一会儿返回,假设不为 null。
  126. final E element = x.item;
  127. //获取指定节点后面的节点
  128. final Node<E> next = x.next;
  129. //获取指定节点前面的节点
  130. final Node<E> prev = x.prev;
  131. //如果前面没有节点,说明 x 是第一个
  132. if (prev == null) {
  133. first = next;
  134. } else {
  135. //前面有节点,让前面节点跨过 x 直接指向 x 后面的节点
  136. prev.next = next;
  137. x.prev = null;
  138. }
  139. //如果后面没有节点,说 x 是最后一个节点
  140. if (next == null) {
  141. last = prev;
  142. } else {
  143. //后面有节点,让后面的节点指向 x 前面的
  144. next.prev = prev;
  145. x.next = null;
  146. }
  147. x.item = null;
  148. size--;
  149. modCount++;
  150. return element;
  151. }
  152. /**
  153. * Returns the (non-null) Node at the specified element index.
  154. * 获取指定位置的节点
  155. */
  156. Node<E> node(int index) {
  157. // assert isElementIndex(index);
  158. //二分一下,如果小于 size 的一半,从头开始遍历
  159. if (index < (size >> 1)) {
  160. Node<E> x = first;
  161. for (int i = 0; i < index; i++)
  162. x = x.next;
  163. return x;
  164. //大于 size 一半,从尾部倒着遍历
  165. } else {
  166. Node<E> x = last;
  167. for (int i = size - 1; i > index; i--)
  168. x = x.prev;
  169. return x;
  170. }
  171. }

4、LinkedList-公开方法-添加方法

  1. /**
  2. * 将指定的元素追加到此列表的末尾。
  3. *
  4. * <p>This method is equivalent to {@link #addLast}.
  5. *
  6. * @param 要附加到此列表的元素
  7. * @return 添加成功则返回true
  8. */
  9. public boolean add(E e) {
  10. linkLast(e);
  11. return true;
  12. }
  13. //在指定位置添加元素
  14. public void add(int index, E element) {
  15. //检查 指示参数是否是迭代器或add操作。
  16. checkPositionIndex(index);
  17. //指定位置也有可能是在尾部
  18. //该元素插入到尾部[作为尾节点]
  19. if (index == size)
  20. linkLast(element);
  21. else
  22. linkBefore(element, node(index)); //在 非空的指定节点 前面插入一个元素
  23. }
  24. //添加一个集合的元素
  25. public boolean addAll(Collection<? extends E> c) {
  26. return addAll(size, c);
  27. }
  28. /**
  29. * 将指定集合中的所有元素插入列表,从指定位置开始。
  30. *
  31. * @param 插入第一个元素的索引
  32. * @param c collection containing elements to be added to this list
  33. * @return {@code true} if this list changed as a result of the call
  34. * @throws IndexOutOfBoundsException {@inheritDoc}
  35. * @throws 如果指定的集合为null,@将引发NullPointerException
  36. */
  37. public boolean addAll(int index, Collection<? extends E> c) {
  38. checkPositionIndex(index);
  39. //把 要添加的集合转成一个 数组
  40. Object[] a = c.toArray();
  41. int numNew = a.length;
  42. if (numNew == 0)
  43. return false;
  44. //创建两个节点,分别指向要插入位置前面和后面的节点
  45. Node<E> pred, succ;
  46. //相等,则说明是要添加到尾部
  47. if (index == size) {
  48. succ = null;
  49. pred = last;
  50. } else {
  51. //要添加到中间, succ 指向 index 位置的节点,pred 指向它前一个
  52. succ = node(index);
  53. pred = succ.prev;
  54. }
  55. //遍历要添加内容的数组
  56. for (Object o : a) {
  57. @SuppressWarnings("unchecked") E e = (E) o;
  58. //创建新节点,头指针指向 pred
  59. Node<E> newNode = new Node<>(pred, e, null);
  60. //如果 pred 为空,说明新建的这个是头节点
  61. if (pred == null)
  62. first = newNode;
  63. else
  64. pred.next = newNode; //pred 指向新建的节点
  65. //pred 后移一位
  66. pred = newNode;
  67. }
  68. //添加完后需要修改尾指针
  69. if (succ == null) {
  70. //如果 succ 为空,说明要插入的位置就是尾部,现在 pred 已经到最后了
  71. last = pred;
  72. } else {
  73. //否则 pred 指向后面的元素
  74. pred.next = succ;
  75. succ.prev = pred;
  76. }
  77. size += numNew;
  78. modCount++;
  79. return true;
  80. }
  81. //添加到头部,时间复杂度为 O(1)
  82. public void addFirst(E e) {
  83. //调用内部方法,将该元素插入到头部[作为头节点]
  84. linkFirst(e);
  85. }
  86. //添加到尾部,时间复杂度为 O(1)
  87. public void addLast(E e) {
  88. //调用内部方法,将该元素插入到尾部[作为尾节点]
  89. linkLast(e);
  90. }
  91. 继承自双端队列(Deque)的添加方法:
  92. //入栈,其实就是在头部添加元素
  93. public void push(E e) {
  94. addFirst(e);
  95. }
  96. //安全的添加操作,在尾部添加
  97. public boolean offer(E e) {
  98. return add(e);
  99. }
  100. //在头部添加
  101. public boolean offerFirst(E e) {
  102. addFirst(e);
  103. return true;
  104. }
  105. //尾部添加
  106. public boolean offerLast(E e) {
  107. addLast(e);
  108. return true;
  109. }

5、LinkedList-公开方法-删除方法

  1. //删除头部节点
  2. public E remove() {
  3. return removeFirst();
  4. }
  5. //删除指定位置节点
  6. public E remove(int index) {
  7. checkElementIndex(index);
  8. return unlink(node(index));
  9. }
  10. //删除包含指定元素的节点,这就得遍历了
  11. public boolean remove(Object o) {
  12. if (o == null) {
  13. //遍历终止条件,不等于 null
  14. for (Node<E> x = first; x != null; x = x.next) {
  15. if (x.item == null) {
  16. unlink(x);
  17. return true;
  18. }
  19. }
  20. } else {
  21. for (Node<E> x = first; x != null; x = x.next) {
  22. if (o.equals(x.item)) {
  23. unlink(x);
  24. return true;
  25. }
  26. }
  27. }
  28. return false;
  29. }
  30. //删除头部元素
  31. public E removeFirst() {
  32. final Node<E> f = first;
  33. if (f == null)
  34. throw new NoSuchElementException();
  35. return unlinkFirst(f);
  36. }
  37. //删除尾部元素
  38. public E removeLast() {
  39. final Node<E> l = last;
  40. if (l == null)
  41. throw new NoSuchElementException();
  42. return unlinkLast(l);
  43. }
  44. //删除首次出现的指定元素,从头遍历
  45. public boolean removeFirstOccurrence(Object o) {
  46. return remove(o);
  47. }
  48. //删除最后一次出现的指定元素,倒过来遍历
  49. public boolean removeLastOccurrence(Object o) {
  50. if (o == null) {
  51. for (Node<E> x = last; x != null; x = x.prev) {
  52. if (x.item == null) {
  53. unlink(x);
  54. return true;
  55. }
  56. }
  57. } else {
  58. for (Node<E> x = last; x != null; x = x.prev) {
  59. if (o.equals(x.item)) {
  60. unlink(x);
  61. return true;
  62. }
  63. }
  64. }
  65. return false;
  66. }
  67. 继承自双端队列(Deque)的删除方法:
  68. public E pop() {
  69. return removeFirst();
  70. }
  71. public E pollFirst() {
  72. final Node<E> f = first;
  73. return (f == null) ? null : unlinkFirst(f);
  74. }
  75. public E pollLast() {
  76. final Node<E> l = last;
  77. return (l == null) ? null : unlinkLast(l);
  78. }

6、LinkedList-公开方法-修改方法

  1. 1、公开的修改方法,只有一个 set :
  2. //找到这个节点,替换数据就好了
  3. public E set(int index, E element) {
  4. checkElementIndex(index);
  5. Node<E> x = node(index);
  6. E oldVal = x.item;
  7. x.item = element;
  8. return oldVal;
  9. }

7、LinkedList-公开方法-查询方法

  1. //挨个遍历,获取第一次出现位置
  2. public int indexOf(Object o) {
  3. int index = 0;
  4. if (o == null) {
  5. for (Node<E> x = first; x != null; x = x.next) {
  6. if (x.item == null)
  7. return index;
  8. index++;
  9. }
  10. } else {
  11. for (Node<E> x = first; x != null; x = x.next) {
  12. if (o.equals(x.item))
  13. return index;
  14. index++;
  15. }
  16. }
  17. return -1;
  18. }
  19. //倒着遍历,查询最后一次出现的位置
  20. public int lastIndexOf(Object o) {
  21. int index = size;
  22. if (o == null) {
  23. for (Node<E> x = last; x != null; x = x.prev) {
  24. index--;
  25. if (x.item == null)
  26. return index;
  27. }
  28. } else {
  29. for (Node<E> x = last; x != null; x = x.prev) {
  30. index--;
  31. if (o.equals(x.item))
  32. return index;
  33. }
  34. }
  35. return -1;
  36. }
  37. //是否包含指定元素
  38. public boolean contains(Object o) {
  39. return indexOf(o) != -1;
  40. }
  41. //获取指定位置的元素,需要遍历
  42. public E get(int index) {
  43. checkElementIndex(index);
  44. return node(index).item;
  45. }
  46. //获取第一个元素,很快
  47. public E getFirst() {
  48. final Node<E> f = first;
  49. if (f == null)
  50. throw new NoSuchElementException();
  51. return f.item;
  52. }
  53. 继承自双端队列(Deque)的查询方法:
  54. //获取第一个,同时删除它
  55. public E poll() {
  56. final Node<E> f = first;
  57. return (f == null) ? null : unlinkFirst(f);
  58. }
  59. //也是获取第一个,和 poll 不同的是不删除
  60. public E peek() {
  61. final Node<E> f = first;
  62. return (f == null) ? null : f.item;
  63. }
  64. //长得一样嘛
  65. public E peekFirst() {
  66. final Node<E> f = first;
  67. return (f == null) ? null : f.item;
  68. }
  69. //最后一个元素,也很快
  70. public E getLast() {
  71. final Node<E> l = last;
  72. if (l == null)
  73. throw new NoSuchElementException();
  74. return l.item;
  75. }
  76. public E peekLast() {
  77. final Node<E> l = last;
  78. return (l == null) ? null : l.item;
  79. }

8、LinkedList-公开方法-清除方法

  1. 1、清除全部元素其实只需要把首尾都置为 null, 这个链表就已经是空的,因为无法访问元素。但是为了避免浪费空间,需要把中间节点都置为 null
  2. public void clear() {
  3. for (Node<E> x = first; x != null; ) {
  4. Node<E> next = x.next;
  5. x.item = null;
  6. x.next = null;
  7. x.prev = null;
  8. x = next;
  9. }
  10. first = last = null;
  11. size = 0;
  12. modCount++;
  13. }

8、LinkedList-迭代器

  1. 1LinkedList,内部实现的迭代器(Iterator)。
  2. 1-1LinkedList 实现了一个倒序迭代器 DescendingIterator
  3. 1-2、还实现了一个 ListIterator ,名叫 ListItr迭代器[???]。
  4. 1-3、还有个 LLSpliterator 继承自 Spliterator JDK8加入的。

8-1、LinkedList-迭代器-DescendingIterator 倒序迭代器

  1. 1DescendingIterator 倒序迭代器:
  2. /**
  3. * @since 1.6
  4. */
  5. public Iterator<E> descendingIterator() {
  6. return new DescendingIterator();
  7. }
  8. /**
  9. * Adapter to provide descending iterators via ListItr.previous
  10. */
  11. private class DescendingIterator implements Iterator<E> {
  12. // ListItr迭代器
  13. private final ListItr itr = new ListItr(size());
  14. public boolean hasNext() {
  15. return itr.hasPrevious();
  16. }
  17. public E next() {
  18. return itr.previous();
  19. }
  20. public void remove() {
  21. itr.remove();
  22. }
  23. }

8-2、LinkedList-迭代器-ListItr迭代器

  1. 1ListItr迭代器
  2. /**
  3. * 返回此列表中元素的列表迭代器。从列表中指定的位置开始。
  4. * 面对如果是并发修改,迭代器会迅速而彻底地失败
  5. *
  6. * @param index index of the first element to be returned from the
  7. * list-iterator (by a call to {@code next})
  8. * @return a ListIterator of the elements in this list (in proper
  9. * sequence), starting at the specified position in the list
  10. * @throws IndexOutOfBoundsException {@inheritDoc}
  11. * @see List#listIterator(int)
  12. */
  13. public ListIterator<E> listIterator(int index) {
  14. checkPositionIndex(index);
  15. return new ListItr(index);
  16. }
  17. private class ListItr implements ListIterator<E> {
  18. private Node<E> lastReturned;
  19. private Node<E> next;
  20. private int nextIndex;
  21. private int expectedModCount = modCount;
  22. ListItr(int index) {
  23. // assert isPositionIndex(index);
  24. // 二分遍历,指定游标位置
  25. next = (index == size) ? null : node(index);
  26. nextIndex = index;
  27. }
  28. public boolean hasNext() {
  29. return nextIndex < size;
  30. }
  31. public E next() {
  32. checkForComodification();
  33. if (!hasNext())
  34. throw new NoSuchElementException();
  35. lastReturned = next;
  36. next = next.next;
  37. nextIndex++;
  38. return lastReturned.item;
  39. }
  40. public boolean hasPrevious() {
  41. return nextIndex > 0;
  42. }
  43. public E previous() {
  44. checkForComodification();
  45. if (!hasPrevious())
  46. throw new NoSuchElementException();
  47. lastReturned = next = (next == null) ? last : next.prev;
  48. nextIndex--;
  49. return lastReturned.item;
  50. }
  51. public int nextIndex() {
  52. return nextIndex;
  53. }
  54. public int previousIndex() {
  55. return nextIndex - 1;
  56. }
  57. public void remove() {
  58. checkForComodification();
  59. if (lastReturned == null)
  60. throw new IllegalStateException();
  61. Node<E> lastNext = lastReturned.next;
  62. unlink(lastReturned);
  63. if (next == lastReturned)
  64. next = lastNext;
  65. else
  66. nextIndex--;
  67. lastReturned = null;
  68. expectedModCount++;
  69. }
  70. public void set(E e) {
  71. if (lastReturned == null)
  72. throw new IllegalStateException();
  73. checkForComodification();
  74. lastReturned.item = e;
  75. }
  76. public void add(E e) {
  77. checkForComodification();
  78. lastReturned = null;
  79. if (next == null)
  80. linkLast(e);
  81. else
  82. linkBefore(e, next);
  83. nextIndex++;
  84. expectedModCount++;
  85. }
  86. public void forEachRemaining(Consumer<? super E> action) {
  87. Objects.requireNonNull(action);
  88. while (modCount == expectedModCount && nextIndex < size) {
  89. action.accept(next.item);
  90. lastReturned = next;
  91. next = next.next;
  92. nextIndex++;
  93. }
  94. checkForComodification();
  95. }
  96. final void checkForComodification() {
  97. if (modCount != expectedModCount)
  98. throw new ConcurrentModificationException();
  99. }
  100. }

8-3、LinkedList-迭代器-LLSpliterator迭代器

  1. 1、使用 Iterator 的时候,我们可以顺序地遍历容器中的元素,使用 Spliterator 的时候,我们可以将元素分割成多份,分别交于不于的线程去遍历,以提高效率。
  2. 2、使用 Spliterator 每次可以处理某个元素集合中的一个元素 不是从 Spliterator 中获取元素,而是使用 tryAdvance() forEachRemaining() 方法对元素应用操作。
  3. 3Spliterator 还可以用于估计其中保存的元素数量,而且还可以像细胞分裂一样变为一分为二。这些新增加的能力让流并行处理代码可以很方便地将工作分布到多个可用线程上完成。
  4. /**
  5. * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
  6. * and <em>fail-fast</em> {@link Spliterator} over the elements in this
  7. * list.
  8. *
  9. * <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
  10. * {@link Spliterator#ORDERED}. Overriding implementations should document
  11. * the reporting of additional characteristic values.
  12. *
  13. * @implNote
  14. * The {@code Spliterator} additionally reports {@link Spliterator#SUBSIZED}
  15. * and implements {@code trySplit} to permit limited parallelism..
  16. *
  17. * @return a {@code Spliterator} over the elements in this list
  18. * @since 1.8
  19. */
  20. @Override
  21. public Spliterator<E> spliterator() {
  22. return new LLSpliterator<E>(this, -1, 0);
  23. }
  24. /** A customized variant of Spliterators.IteratorSpliterator */
  25. static final class LLSpliterator<E> implements Spliterator<E> {
  26. static final int BATCH_UNIT = 1 << 10; // batch array size increment
  27. static final int MAX_BATCH = 1 << 25; // max batch array size;
  28. final LinkedList<E> list; // null OK unless traversed
  29. Node<E> current; // current node; null until initialized
  30. int est; // size estimate; -1 until first needed
  31. int expectedModCount; // initialized when est set
  32. int batch; // batch size for splits
  33. LLSpliterator(LinkedList<E> list, int est, int expectedModCount) {
  34. this.list = list;
  35. this.est = est;
  36. this.expectedModCount = expectedModCount;
  37. }
  38. final int getEst() {
  39. int s; // force initialization
  40. final LinkedList<E> lst;
  41. if ((s = est) < 0) {
  42. if ((lst = list) == null)
  43. s = est = 0;
  44. else {
  45. expectedModCount = lst.modCount;
  46. current = lst.first;
  47. s = est = lst.size;
  48. }
  49. }
  50. return s;
  51. }
  52. public long estimateSize() { return (long) getEst(); }
  53. public Spliterator<E> trySplit() {
  54. Node<E> p;
  55. int s = getEst();
  56. if (s > 1 && (p = current) != null) {
  57. int n = batch + BATCH_UNIT;
  58. if (n > s)
  59. n = s;
  60. if (n > MAX_BATCH)
  61. n = MAX_BATCH;
  62. Object[] a = new Object[n];
  63. int j = 0;
  64. do { a[j++] = p.item; } while ((p = p.next) != null && j < n);
  65. current = p;
  66. batch = j;
  67. est = s - j;
  68. return Spliterators.spliterator(a, 0, j, Spliterator.ORDERED);
  69. }
  70. return null;
  71. }
  72. public void forEachRemaining(Consumer<? super E> action) {
  73. Node<E> p; int n;
  74. if (action == null) throw new NullPointerException();
  75. if ((n = getEst()) > 0 && (p = current) != null) {
  76. current = null;
  77. est = 0;
  78. do {
  79. E e = p.item;
  80. p = p.next;
  81. action.accept(e);
  82. } while (p != null && --n > 0);
  83. }
  84. if (list.modCount != expectedModCount)
  85. throw new ConcurrentModificationException();
  86. }
  87. public boolean tryAdvance(Consumer<? super E> action) {
  88. Node<E> p;
  89. if (action == null) throw new NullPointerException();
  90. if (getEst() > 0 && (p = current) != null) {
  91. --est;
  92. E e = p.item;
  93. current = p.next;
  94. action.accept(e);
  95. if (list.modCount != expectedModCount)
  96. throw new ConcurrentModificationException();
  97. return true;
  98. }
  99. return false;
  100. }
  101. public int characteristics() {
  102. return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
  103. }
  104. }

9、LinkedList-源码分析-总结

  1. 1、概念知识-总结
  2. 1-1LinkedList 实际上是通过双向链表去实现的。
  3. 它包含一个非常重要的内部类:NodeNode是双向链表节点所对应的数据结构,它包括的属性有:当前节点所包含的值,上一个节点,下一个节点。
  4. 1-2、从LinkedList的实现方式中可以发现,它不存在LinkedList容量不足的问题。
  5. 1-3LinkedList的克隆函数,即是将全部元素克隆到一个新的LinkedList对象中。
  6. 1-4、由于LinkedList实现了Deque,而Deque接口定义了在双端队列两端访问元素的方法。提供插入、移除和检查元素的方法。每种方法都存在两种形式:一种形式在操作失败时抛出异常,另一种形式返回一个特殊值(null false,具体取决于操作)
  7. 1-4-1、总结如下:
  8. 第一个元素(头部) 最后一个元素(尾部)
  9. 抛出异常 特殊值 抛出异常 特殊值
  10. 插入 addFirst(e) offerFirst(e) addLast(e) offerLast(e)
  11. 移除 removeFirst() pollFirst() removeLast() pollLast()
  12. 检查 getFirst() peekFirst() getLast() peekLast()
  13. 1-5LinkedList可以作为FIFO(先进先出)的队列,作为FIFO的队列时,如下的方法等价:
  14. 队列方法 等效方法
  15. add(e) addLast(e)
  16. offer(e) offerLast(e)
  17. remove() removeFirst()
  18. poll() pollFirst()
  19. element() getFirst()
  20. peek() peekFirst()
  21. 1-6LinkedList可以作为LIFO(后进先出)的栈,作为LIFO的栈时,如下的方法等价:
  22. 栈方法 等效方法
  23. push(e) addFirst(e)
  24. pop() removeFirst()
  25. peek() peekFirst()
  26. 2、对LinkedList遍历,使用foreach方式,而不是for循环方式
  1. https://www.cnblogs.com/skywang12345/p/3308807.html
  2. https://blog.csdn.net/u011240877/article/details/52876543