资讯专栏INFORMATION COLUMN

站在巨人肩膀上看源码-LinkedList

learn_shifeng / 1362人阅读

摘要:在阅读源码之前,我们先对的整体实现进行大致说明实际上是通过双向链表去实现的。获取的最后一个元素由于是双向链表而表头不包含数据。实际上是判断双向链表的当前节点是否达到开头反向迭代器获取下一个元素。

第1部分 LinkedList介绍 LinkedList简介

LinkedList 是一个继承于AbstractSequentialList的双向链表。它也可以被当作堆栈、队列或双端队列进行操作。
LinkedList 实现 List 接口,能对它进行队列操作。
LinkedList 实现 Deque 接口,即能将LinkedList当作双端队列使用。
LinkedList 实现了Cloneable接口,即覆盖了函数clone(),能克隆。
LinkedList 实现java.io.Serializable接口,这意味着LinkedList支持序列化,能通过序列化去传输。
LinkedList 是非同步的。

LinkedList构造函数

</>复制代码

  1. // 默认构造函数
  2. LinkedList()
  3. // 创建一个LinkedList,保护Collection中的全部元素。
  4. LinkedList(Collection collection)
LinkedList的API

</>复制代码

  1. LinkedList的API
  2. boolean add(E object)
  3. void add(int location, E object)
  4. boolean addAll(Collection collection)
  5. boolean addAll(int location, Collection collection)
  6. void addFirst(E object)
  7. void addLast(E object)
  8. void clear()
  9. Object clone()
  10. boolean contains(Object object)
  11. Iterator descendingIterator()
  12. E element()
  13. E get(int location)
  14. E getFirst()
  15. E getLast()
  16. int indexOf(Object object)
  17. int lastIndexOf(Object object)
  18. ListIterator listIterator(int location)
  19. boolean offer(E o)
  20. boolean offerFirst(E e)
  21. boolean offerLast(E e)
  22. E peek()
  23. E peekFirst()
  24. E peekLast()
  25. E poll()
  26. E pollFirst()
  27. E pollLast()
  28. E pop()
  29. void push(E e)
  30. E remove()
  31. E remove(int location)
  32. boolean remove(Object object)
  33. E removeFirst()
  34. boolean removeFirstOccurrence(Object o)
  35. E removeLast()
  36. boolean removeLastOccurrence(Object o)
  37. E set(int location, E object)
  38. int size()
  39. T[] toArray(T[] contents)
  40. Object[] toArray()
AbstractSequentialList简介

在介绍LinkedList的源码之前,先介绍一下AbstractSequentialList。毕竟,LinkedList是AbstractSequentialList的子类。

AbstractSequentialList 实现了get(int index)、set(int index, E element)、add(int index, E element) 和 remove(int index)这些函数。这些接口都是随机访问List的,LinkedList是双向链表;既然它继承于AbstractSequentialList,就相当于已经实现了“get(int index)这些接口”。

此外,我们若需要通过AbstractSequentialList自己实现一个列表,只需要扩展此类,并提供 listIterator() 和 size() 方法的实现即可。若要实现不可修改的列表,则需要实现列表迭代器的 hasNext、next、hasPrevious、previous 和 index 方法即可。
inkedList实际上是通过双向链表去实现的。既然是双向链表,那么它的顺序访问会非常高效,而随机访问效率比较低。

第2部分 LinkedList数据结构 LinkedList的继承关系

</>复制代码

  1. java.lang.Object
  2. java.util.AbstractCollection
  3. java.util.AbstractList
  4. java.util.AbstractSequentialList
  5. java.util.LinkedList
  6. public class LinkedList
  7. extends AbstractSequentialList
  8. implements List, Deque, Cloneable, java.io.Serializable {}
  9. LinkedList与Collection关系如下图:
  10. ---------------------------
  11. ![272345393446232.jpg][1]
  12. LinkedList的本质是双向链表。
  13. (01) LinkedList继承于AbstractSequentialList,并且实现了Dequeue接口。
  14. (02) LinkedList包含两个重要的成员:header 和 size。
  15.   header是双向链表的表头,它是双向链表节点所对应的类Entry的实例。Entry中包含成员变量: previous, next, element。其中,previous是该节点的上一个节点,next是该节点的下一个节点,element是该节点所包含的值。
  16.   size是双向链表中节点的个数。
  17. (03)LinkedList数据结构;
  18. ![616953-20160322214504120-1558870057.png][2]
  19. [1]: /img/bVbbtxf
  20. [2]: /img/bVbbtxw
  21. 说明:如上图所示,LinkedList底层使用的双向链表结构,有一个头结点和一个尾结点,双向链表意味着我们可以从头开始正向遍历,或者是从尾开始逆向遍历,并且可以针对头部和尾部进行相应的操作。
第3部分 LinkedList源码解析

为了更了解LinkedList的原理,下面对LinkedList源码代码作出分析。

在阅读源码之前,我们先对LinkedList的整体实现进行大致说明:

</>复制代码

  1. LinkedList实际上是通过双向链表去实现的。既然是双向链表,那么它的顺序访问会非常高效,而随机访问效率比较低。
  2. 既然LinkedList是通过双向链表的,但是它也实现了List接口{也就是说,它实现了get(int location)、remove(int location)等“根据索引值来获取、删除节点的函数”}。LinkedList是如何实现List的这些接口的,如何将“双向链表和索引值联系起来的”?
  3. 实际原理非常简单,它就是通过一个计数索引值来实现的。例如,当我们调用get(int location)时,首先会比较“location”和“双向链表长度的1/2”;若前者大,则从链表头开始往后查找,直到location位置;否则,从链表末尾开始先前查找,直到location位置。

这就是“双线链表和索引值联系起来”的方法。

好了,接下来开始阅读源码(只要理解双向链表,那么LinkedList的源码很容易理解的)

</>复制代码

  1. package java.util;
  2. public class LinkedList
  3. extends AbstractSequentialList
  4. implements List, Deque, Cloneable, java.io.Serializable
  5. {
  6. // 链表的表头,表头不包含任何数据。Entry是个链表类数据结构。
  7. private transient Entry header = new Entry(null, null, null);
  8. // LinkedList中元素个数
  9. private transient int size = 0;
  10. // 默认构造函数:创建一个空的链表
  11. public LinkedList() {
  12. header.next = header.previous = header;
  13. }
  14. // 包含“集合”的构造函数:创建一个包含“集合”的LinkedList
  15. public LinkedList(Collection c) {
  16. this();
  17. addAll(c);
  18. }
  19. // 获取LinkedList的第一个元素
  20. public E getFirst() {
  21. if (size==0)
  22. throw new NoSuchElementException();
  23. // 链表的表头header中不包含数据。
  24. // 这里返回header所指下一个节点所包含的数据。
  25. return header.next.element;
  26. }
  27. // 获取LinkedList的最后一个元素
  28. public E getLast() {
  29. if (size==0)
  30. throw new NoSuchElementException();
  31. // 由于LinkedList是双向链表;而表头header不包含数据。
  32. // 因而,这里返回表头header的前一个节点所包含的数据。
  33. return header.previous.element;
  34. }
  35. // 删除LinkedList的第一个元素
  36. public E removeFirst() {
  37. return remove(header.next);
  38. }
  39. // 删除LinkedList的最后一个元素
  40. public E removeLast() {
  41. return remove(header.previous);
  42. }
  43. // 将元素添加到LinkedList的起始位置
  44. public void addFirst(E e) {
  45. addBefore(e, header.next);
  46. }
  47. // 将元素添加到LinkedList的结束位置
  48. public void addLast(E e) {
  49. addBefore(e, header);
  50. }
  51. // 判断LinkedList是否包含元素(o)
  52. public boolean contains(Object o) {
  53. return indexOf(o) != -1;
  54. }
  55. // 返回LinkedList的大小
  56. public int size() {
  57. return size;
  58. }
  59. // 将元素(E)添加到LinkedList中
  60. public boolean add(E e) {
  61. // 将节点(节点数据是e)添加到表头(header)之前。
  62. // 即,将节点添加到双向链表的末端。
  63. addBefore(e, header);
  64. return true;
  65. }
  66. // 从LinkedList中删除元素(o)
  67. // 从链表开始查找,如存在元素(o)则删除该元素并返回true
  68. // 否则,返回false
  69. public boolean remove(Object o) {
  70. if (o==null) {
  71. // 若o为null的删除情况
  72. for (Entry e = header.next; e != header; e = e.next) {
  73. if (e.element==null) {
  74. remove(e);
  75. return true;
  76. }
  77. }
  78. } else {
  79. // 若o不为null的删除情况
  80. for (Entry e = header.next; e != header; e = e.next) {
  81. if (o.equals(e.element)) {
  82. remove(e);
  83. return true;
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. // 将“集合(c)”添加到LinkedList中。
  90. // 实际上,是从双向链表的末尾开始,将“集合(c)”添加到双向链表中。
  91. public boolean addAll(Collection c) {
  92. return addAll(size, c);
  93. }
  94. // 从双向链表的index开始,将“集合(c)”添加到双向链表中。
  95. public boolean addAll(int index, Collection c) {
  96. if (index < 0 || index > size)
  97. throw new IndexOutOfBoundsException("Index: "+index+
  98. ", Size: "+size);
  99. Object[] a = c.toArray();
  100. // 获取集合的长度
  101. int numNew = a.length;
  102. if (numNew==0)
  103. return false;
  104. modCount++;
  105. // 设置“当前要插入节点的后一个节点”
  106. Entry successor = (index==size ? header : entry(index));
  107. // 设置“当前要插入节点的前一个节点”
  108. Entry predecessor = successor.previous;
  109. // 将集合(c)全部插入双向链表中
  110. for (int i=0; i e = new Entry((E)a[i], successor, predecessor);
  111. predecessor.next = e;
  112. predecessor = e;
  113. }
  114. successor.previous = predecessor;
  115. // 调整LinkedList的实际大小
  116. size += numNew;
  117. return true;
  118. }
  119. // 清空双向链表
  120. public void clear() {
  121. Entry e = header.next;
  122. // 从表头开始,逐个向后遍历;对遍历到的节点执行一下操作:
  123. // (01) 设置前一个节点为null
  124. // (02) 设置当前节点的内容为null
  125. // (03) 设置后一个节点为“新的当前节点”
  126. while (e != header) {
  127. Entry next = e.next;
  128. e.next = e.previous = null;
  129. e.element = null;
  130. e = next;
  131. }
  132. header.next = header.previous = header;
  133. // 设置大小为0
  134. size = 0;
  135. modCount++;
  136. }
  137. // 返回LinkedList指定位置的元素
  138. public E get(int index) {
  139. return entry(index).element;
  140. }
  141. // 设置index位置对应的节点的值为element
  142. public E set(int index, E element) {
  143. Entry e = entry(index);
  144. E oldVal = e.element;
  145. e.element = element;
  146. return oldVal;
  147. }
  148. // 在index前添加节点,且节点的值为element
  149. public void add(int index, E element) {
  150. addBefore(element, (index==size ? header : entry(index)));
  151. }
  152. // 删除index位置的节点
  153. public E remove(int index) {
  154. return remove(entry(index));
  155. }
  156. // 获取双向链表中指定位置的节点
  157. private Entry entry(int index) {
  158. if (index < 0 || index >= size)
  159. throw new IndexOutOfBoundsException("Index: "+index+
  160. ", Size: "+size);
  161. Entry e = header;
  162. // 获取index处的节点。
  163. // 若index < 双向链表长度的1/2,则从前先后查找;
  164. // 否则,从后向前查找。
  165. if (index < (size >> 1)) {
  166. for (int i = 0; i <= index; i++)
  167. e = e.next;
  168. } else {
  169. for (int i = size; i > index; i--)
  170. e = e.previous;
  171. }
  172. return e;
  173. }
  174. // 从前向后查找,返回“值为对象(o)的节点对应的索引”
  175. // 不存在就返回-1
  176. public int indexOf(Object o) {
  177. int index = 0;
  178. if (o==null) {
  179. for (Entry e = header.next; e != header; e = e.next) {
  180. if (e.element==null)
  181. return index;
  182. index++;
  183. }
  184. } else {
  185. for (Entry e = header.next; e != header; e = e.next) {
  186. if (o.equals(e.element))
  187. return index;
  188. index++;
  189. }
  190. }
  191. return -1;
  192. }
  193. // 从后向前查找,返回“值为对象(o)的节点对应的索引”
  194. // 不存在就返回-1
  195. public int lastIndexOf(Object o) {
  196. int index = size;
  197. if (o==null) {
  198. for (Entry e = header.previous; e != header; e = e.previous) {
  199. index--;
  200. if (e.element==null)
  201. return index;
  202. }
  203. } else {
  204. for (Entry e = header.previous; e != header; e = e.previous) {
  205. index--;
  206. if (o.equals(e.element))
  207. return index;
  208. }
  209. }
  210. return -1;
  211. }
  212. // 返回第一个节点
  213. // 若LinkedList的大小为0,则返回null
  214. public E peek() {
  215. if (size==0)
  216. return null;
  217. return getFirst();
  218. }
  219. // 返回第一个节点
  220. // 若LinkedList的大小为0,则抛出异常
  221. public E element() {
  222. return getFirst();
  223. }
  224. // 删除并返回第一个节点
  225. // 若LinkedList的大小为0,则返回null
  226. public E poll() {
  227. if (size==0)
  228. return null;
  229. return removeFirst();
  230. }
  231. // 将e添加双向链表末尾
  232. public boolean offer(E e) {
  233. return add(e);
  234. }
  235. // 将e添加双向链表开头
  236. public boolean offerFirst(E e) {
  237. addFirst(e);
  238. return true;
  239. }
  240. // 将e添加双向链表末尾
  241. public boolean offerLast(E e) {
  242. addLast(e);
  243. return true;
  244. }
  245. // 返回第一个节点
  246. // 若LinkedList的大小为0,则返回null
  247. public E peekFirst() {
  248. if (size==0)
  249. return null;
  250. return getFirst();
  251. }
  252. // 返回最后一个节点
  253. // 若LinkedList的大小为0,则返回null
  254. public E peekLast() {
  255. if (size==0)
  256. return null;
  257. return getLast();
  258. }
  259. // 删除并返回第一个节点
  260. // 若LinkedList的大小为0,则返回null
  261. public E pollFirst() {
  262. if (size==0)
  263. return null;
  264. return removeFirst();
  265. }
  266. // 删除并返回最后一个节点
  267. // 若LinkedList的大小为0,则返回null
  268. public E pollLast() {
  269. if (size==0)
  270. return null;
  271. return removeLast();
  272. }
  273. // 将e插入到双向链表开头
  274. public void push(E e) {
  275. addFirst(e);
  276. }
  277. // 删除并返回第一个节点
  278. public E pop() {
  279. return removeFirst();
  280. }
  281. // 从LinkedList开始向后查找,删除第一个值为元素(o)的节点
  282. // 从链表开始查找,如存在节点的值为元素(o)的节点,则删除该节点
  283. public boolean removeFirstOccurrence(Object o) {
  284. return remove(o);
  285. }
  286. // 从LinkedList末尾向前查找,删除第一个值为元素(o)的节点
  287. // 从链表开始查找,如存在节点的值为元素(o)的节点,则删除该节点
  288. public boolean removeLastOccurrence(Object o) {
  289. if (o==null) {
  290. for (Entry e = header.previous; e != header; e = e.previous) {
  291. if (e.element==null) {
  292. remove(e);
  293. return true;
  294. }
  295. }
  296. } else {
  297. for (Entry e = header.previous; e != header; e = e.previous) {
  298. if (o.equals(e.element)) {
  299. remove(e);
  300. return true;
  301. }
  302. }
  303. }
  304. return false;
  305. }
  306. // 返回“index到末尾的全部节点”对应的ListIterator对象(List迭代器)
  307. public ListIterator listIterator(int index) {
  308. return new ListItr(index);
  309. }
  310. // List迭代器
  311. private class ListItr implements ListIterator {
  312. // 上一次返回的节点
  313. private Entry lastReturned = header;
  314. // 下一个节点
  315. private Entry next;
  316. // 下一个节点对应的索引值
  317. private int nextIndex;
  318. // 期望的改变计数。用来实现fail-fast机制。
  319. private int expectedModCount = modCount;
  320. // 构造函数。
  321. // 从index位置开始进行迭代
  322. ListItr(int index) {
  323. // index的有效性处理
  324. if (index < 0 || index > size)
  325. throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size);
  326. // 若 “index 小于 ‘双向链表长度的一半’”,则从第一个元素开始往后查找;
  327. // 否则,从最后一个元素往前查找。
  328. if (index < (size >> 1)) {
  329. next = header.next;
  330. for (nextIndex=0; nextIndexindex; nextIndex--)
  331. next = next.previous;
  332. }
  333. }
  334. // 是否存在下一个元素
  335. public boolean hasNext() {
  336. // 通过元素索引是否等于“双向链表大小”来判断是否达到最后。
  337. return nextIndex != size;
  338. }
  339. // 获取下一个元素
  340. public E next() {
  341. checkForComodification();
  342. if (nextIndex == size)
  343. throw new NoSuchElementException();
  344. lastReturned = next;
  345. // next指向链表的下一个元素
  346. next = next.next;
  347. nextIndex++;
  348. return lastReturned.element;
  349. }
  350. // 是否存在上一个元素
  351. public boolean hasPrevious() {
  352. // 通过元素索引是否等于0,来判断是否达到开头。
  353. return nextIndex != 0;
  354. }
  355. // 获取上一个元素
  356. public E previous() {
  357. if (nextIndex == 0)
  358. throw new NoSuchElementException();
  359. // next指向链表的上一个元素
  360. lastReturned = next = next.previous;
  361. nextIndex--;
  362. checkForComodification();
  363. return lastReturned.element;
  364. }
  365. // 获取下一个元素的索引
  366. public int nextIndex() {
  367. return nextIndex;
  368. }
  369. // 获取上一个元素的索引
  370. public int previousIndex() {
  371. return nextIndex-1;
  372. }
  373. // 删除当前元素。
  374. // 删除双向链表中的当前节点
  375. public void remove() {
  376. checkForComodification();
  377. Entry lastNext = lastReturned.next;
  378. try {
  379. LinkedList.this.remove(lastReturned);
  380. } catch (NoSuchElementException e) {
  381. throw new IllegalStateException();
  382. }
  383. if (next==lastReturned)
  384. next = lastNext;
  385. else
  386. nextIndex--;
  387. lastReturned = header;
  388. expectedModCount++;
  389. }
  390. // 设置当前节点为e
  391. public void set(E e) {
  392. if (lastReturned == header)
  393. throw new IllegalStateException();
  394. checkForComodification();
  395. lastReturned.element = e;
  396. }
  397. // 将e添加到当前节点的前面
  398. public void add(E e) {
  399. checkForComodification();
  400. lastReturned = header;
  401. addBefore(e, next);
  402. nextIndex++;
  403. expectedModCount++;
  404. }
  405. // 判断 “modCount和expectedModCount是否相等”,依次来实现fail-fast机制。
  406. final void checkForComodification() {
  407. if (modCount != expectedModCount)
  408. throw new ConcurrentModificationException();
  409. }
  410. }
  411. // 双向链表的节点所对应的数据结构。
  412. // 包含3部分:上一节点,下一节点,当前节点值。
  413. private static class Entry {
  414. // 当前节点所包含的值
  415. E element;
  416. // 下一个节点
  417. Entry next;
  418. // 上一个节点
  419. Entry previous;
  420. /**
  421. * 链表节点的构造函数。
  422. * 参数说明:
  423. * element —— 节点所包含的数据
  424. * next —— 下一个节点
  425. * previous —— 上一个节点
  426. */
  427. Entry(E element, Entry next, Entry previous) {
  428. this.element = element;
  429. this.next = next;
  430. this.previous = previous;
  431. }
  432. }
  433. // 将节点(节点数据是e)添加到entry节点之前。
  434. private Entry addBefore(E e, Entry entry) {
  435. // 新建节点newEntry,将newEntry插入到节点e之前;并且设置newEntry的数据是e
  436. Entry newEntry = new Entry(e, entry, entry.previous);
  437. newEntry.previous.next = newEntry;
  438. newEntry.next.previous = newEntry;
  439. // 修改LinkedList大小
  440. size++;
  441. // 修改LinkedList的修改统计数:用来实现fail-fast机制。
  442. modCount++;
  443. return newEntry;
  444. }
  445. // 将节点从链表中删除
  446. private E remove(Entry e) {
  447. if (e == header)
  448. throw new NoSuchElementException();
  449. E result = e.element;
  450. e.previous.next = e.next;
  451. e.next.previous = e.previous;
  452. e.next = e.previous = null;
  453. e.element = null;
  454. size--;
  455. modCount++;
  456. return result;
  457. }
  458. // 反向迭代器
  459. public Iterator descendingIterator() {
  460. return new DescendingIterator();
  461. }
  462. // 反向迭代器实现类。
  463. private class DescendingIterator implements Iterator {
  464. final ListItr itr = new ListItr(size());
  465. // 反向迭代器是否下一个元素。
  466. // 实际上是判断双向链表的当前节点是否达到开头
  467. public boolean hasNext() {
  468. return itr.hasPrevious();
  469. }
  470. // 反向迭代器获取下一个元素。
  471. // 实际上是获取双向链表的前一个节点
  472. public E next() {
  473. return itr.previous();
  474. }
  475. // 删除当前节点
  476. public void remove() {
  477. itr.remove();
  478. }
  479. }
  480. // 返回LinkedList的Object[]数组
  481. public Object[] toArray() {
  482. // 新建Object[]数组
  483. Object[] result = new Object[size];
  484. int i = 0;
  485. // 将链表中所有节点的数据都添加到Object[]数组中
  486. for (Entry e = header.next; e != header; e = e.next)
  487. result[i++] = e.element;
  488. return result;
  489. }
  490. // 返回LinkedList的模板数组。所谓模板数组,即可以将T设为任意的数据类型
  491. public T[] toArray(T[] a) {
  492. // 若数组a的大小 < LinkedList的元素个数(意味着数组a不能容纳LinkedList中全部元素)
  493. // 则新建一个T[]数组,T[]的大小为LinkedList大小,并将该T[]赋值给a。
  494. if (a.length < size)
  495. a = (T[])java.lang.reflect.Array.newInstance(
  496. a.getClass().getComponentType(), size);
  497. // 将链表中所有节点的数据都添加到数组a中
  498. int i = 0;
  499. Object[] result = a;
  500. for (Entry e = header.next; e != header; e = e.next)
  501. result[i++] = e.element;
  502. if (a.length > size)
  503. a[size] = null;
  504. return a;
  505. }
  506. // 克隆函数。返回LinkedList的克隆对象。
  507. public Object clone() {
  508. LinkedList clone = null;
  509. // 克隆一个LinkedList克隆对象
  510. try {
  511. clone = (LinkedList) super.clone();
  512. } catch (CloneNotSupportedException e) {
  513. throw new InternalError();
  514. }
  515. // 新建LinkedList表头节点
  516. clone.header = new Entry(null, null, null);
  517. clone.header.next = clone.header.previous = clone.header;
  518. clone.size = 0;
  519. clone.modCount = 0;
  520. // 将链表中所有节点的数据都添加到克隆对象中
  521. for (Entry e = header.next; e != header; e = e.next)
  522. clone.add(e.element);
  523. return clone;
  524. }
  525. // java.io.Serializable的写入函数
  526. // 将LinkedList的“容量,所有的元素值”都写入到输出流中
  527. private void writeObject(java.io.ObjectOutputStream s)
  528. throws java.io.IOException {
  529. // Write out any hidden serialization magic
  530. s.defaultWriteObject();
  531. // 写入“容量”
  532. s.writeInt(size);
  533. // 将链表中所有节点的数据都写入到输出流中
  534. for (Entry e = header.next; e != header; e = e.next)
  535. s.writeObject(e.element);
  536. }
  537. // java.io.Serializable的读取函数:根据写入方式反向读出
  538. // 先将LinkedList的“容量”读出,然后将“所有的元素值”读出
  539. private void readObject(java.io.ObjectInputStream s)
  540. throws java.io.IOException, ClassNotFoundException {
  541. // Read in any hidden serialization magic
  542. s.defaultReadObject();
  543. // 从输入流中读取“容量”
  544. int size = s.readInt();
  545. // 新建链表表头节点
  546. header = new Entry(null, null, null);
  547. header.next = header.previous = header;
  548. // 从输入流中将“所有的元素值”并逐个添加到链表中
  549. for (int i=0; i
  550. 总结:
    (01) LinkedList 实际上是通过双向链表去实现的。它包含一个非常重要的内部类:Entry。Entry是双向链表节点所对应的数据结构,它包括的属性有:当前节点所包含的值,上一个节点,下一个节点。
    (02) 从LinkedList的实现方式中可以发现,它不存在LinkedList容量不足的问题。
    (03) LinkedList的克隆函数,即是将全部元素克隆到一个新的LinkedList对象中。
    (04) LinkedList实现java.io.Serializable。当写入到输出流时,先写入“容量”,再依次写入“每一个节点保护的值”;当读出输入流时,先读取“容量”,再依次读取“每一个元素”。
    (05) 由于LinkedList实现了Deque,而Deque接口定义了在双端队列两端访问元素的方法。提供插入、移除和检查元素的方法。每种方法都存在两种形式:一种形式在操作失败时抛出异常,另一种形式返回一个特殊值(nullfalse,具体取决于操作)。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69564.html

相关文章

  • 站在巨人肩膀上看源码-ArrayList

    摘要:源码剖析的源码如下加入了比较详细的注释序列版本号基于该数组实现,用该数组保存数据中实际数据的数量带容量大小的构造函数。该方法被标记了,调用了系统的代码,在中是看不到的,但在中可以看到其源码。 ArrayList简介 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存。ArrayList不是线程安全的,只能用在单线程环境下,多...

    ThinkSNS 评论0 收藏0
  • 站在巨人肩膀上看源码-HashSet

    摘要:实际运行上面程序将看到程序输出,这是因为判断两个对象相等的标准除了要求通过方法比较返回之外,还要求两个对象的返回值相等。通常来说,所有参与计算返回值的关键属性,都应该用于作为比较的标准。 1.HashSet概述:   HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持。它不保证set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用null元素。Hash...

    DevTTL 评论0 收藏0
  • 站在巨人肩膀上看源码-Map

    摘要:在学习的实现类是基于实现的前,先来介绍下接口及其下的子接口先看下的架构图如上图是映射接口,中存储的内容是键值对。是继承于的接口。中的内容是排序的键值对,排序的方法是通过比较器。 Map 在学习Set(Set的实现类是基于Map实现的)、HashMap、TreeMap前,先来介绍下Map接口及其下的子接口.先看下Map的架构图:showImg(https://segmentfault.c...

    xiaotianyi 评论0 收藏0
  • 站在巨人肩膀上看源码-HashMap(基于jdk1.8)

    摘要:而中,采用数组链表红黑树实现,当链表长度超过阈值时,将链表转换为红黑树,这样大大减少了查找时间。到了,当同一个值的节点数不小于时,不再采用单链表形式存储,而是采用红黑树,如下图所示。 一. HashMap概述 在JDK1.8之前,HashMap采用数组+链表实现,即使用链表处理冲突,同一hash值的节点都存储在一个链表里。但是当位于一个桶中的元素较多,即hash值相等的元素较多时,通过...

    刘玉平 评论0 收藏0
  • 站在巨人肩膀上看源码-ConcurrentHashMap

    摘要:一出现背景线程不安全的因为多线程环境下,使用进行操作会引起死循环,导致利用率接近,所以在并发情况下不能使用。是由数组结构和数组结构组成。用来表示需要进行的界限值。也是,这使得能够读取到最新的值而不需要同步。 一、出现背景 1、线程不安全的HashMap 因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap。...

    n7then 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<