1. 程式人生 > >Java 集合系列05之 LinkedList詳細介紹(原始碼解析)和使用示例

Java 集合系列05之 LinkedList詳細介紹(原始碼解析)和使用示例

  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     private transient Entry<E> header = new
Entry<E>(null, null, null); 9 10 // LinkedList中元素個數 11 private transient int size = 0; 12 13 // 預設建構函式:建立一個空的連結串列 14 public LinkedList() { 15 header.next = header.previous = header; 16 } 17 18 // 包含“集合”的建構函式:建立一個包含“集合”的LinkedList 19 public
LinkedList(Collection<? extends E> c) { 20 this(); 21 addAll(c); 22 } 23 24 // 獲取LinkedList的第一個元素 25 public E getFirst() { 26 if (size==0) 27 throw new NoSuchElementException(); 28 29 // 連結串列的表頭header中不包含資料。 30 // 這裡返回header所指下一個節點所包含的資料。
31 return header.next.element; 32 } 33 34 // 獲取LinkedList的最後一個元素 35 public E getLast() { 36 if (size==0) 37 throw new NoSuchElementException(); 38 39 // 由於LinkedList是雙向連結串列;而表頭header不包含資料。 40 // 因而,這裡返回表頭header的前一個節點所包含的資料。 41 return header.previous.element; 42 } 43 44 // 刪除LinkedList的第一個元素 45 public E removeFirst() { 46 return remove(header.next); 47 } 48 49 // 刪除LinkedList的最後一個元素 50 public E removeLast() { 51 return remove(header.previous); 52 } 53 54 // 將元素新增到LinkedList的起始位置 55 public void addFirst(E e) { 56 addBefore(e, header.next); 57 } 58 59 // 將元素新增到LinkedList的結束位置 60 public void addLast(E e) { 61 addBefore(e, header); 62 } 63 64 // 判斷LinkedList是否包含元素(o) 65 public boolean contains(Object o) { 66 return indexOf(o) != -1; 67 } 68 69 // 返回LinkedList的大小 70 public int size() { 71 return size; 72 } 73 74 // 將元素(E)新增到LinkedList中 75 public boolean add(E e) { 76 // 將節點(節點資料是e)新增到表頭(header)之前。 77 // 即,將節點新增到雙向連結串列的末端。 78 addBefore(e, header); 79 return true; 80 } 81 82 // 從LinkedList中刪除元素(o) 83 // 從連結串列開始查詢,如存在元素(o)則刪除該元素並返回true; 84 // 否則,返回false。 85 public boolean remove(Object o) { 86 if (o==null) { 87 // 若o為null的刪除情況 88 for (Entry<E> e = header.next; e != header; e = e.next) { 89 if (e.element==null) { 90 remove(e); 91 return true; 92 } 93 } 94 } else { 95 // 若o不為null的刪除情況 96 for (Entry<E> e = header.next; e != header; e = e.next) { 97 if (o.equals(e.element)) { 98 remove(e); 99 return true; 100 } 101 } 102 } 103 return false; 104 } 105 106 // 將“集合(c)”新增到LinkedList中。 107 // 實際上,是從雙向連結串列的末尾開始,將“集合(c)”新增到雙向連結串列中。 108 public boolean addAll(Collection<? extends E> c) { 109 return addAll(size, c); 110 } 111 112 // 從雙向連結串列的index開始,將“集合(c)”新增到雙向連結串列中。 113 public boolean addAll(int index, Collection<? extends E> c) { 114 if (index < 0 || index > size) 115 throw new IndexOutOfBoundsException("Index: "+index+ 116 ", Size: "+size); 117 Object[] a = c.toArray(); 118 // 獲取集合的長度 119 int numNew = a.length; 120 if (numNew==0) 121 return false; 122 modCount++; 123 124 // 設定“當前要插入節點的後一個節點” 125 Entry<E> successor = (index==size ? header : entry(index)); 126 // 設定“當前要插入節點的前一個節點” 127 Entry<E> predecessor = successor.previous; 128 // 將集合(c)全部插入雙向連結串列中 129 for (int i=0; i<numNew; i++) { 130 Entry<E> e = new Entry<E>((E)a[i], successor, predecessor); 131 predecessor.next = e; 132 predecessor = e; 133 } 134 successor.previous = predecessor; 135 136 // 調整LinkedList的實際大小 137 size += numNew; 138 return true; 139 } 140 141 // 清空雙向連結串列 142 public void clear() { 143 Entry<E> e = header.next; 144 // 從表頭開始,逐個向後遍歷;對遍歷到的節點執行一下操作: 145 // (01) 設定前一個節點為null 146 // (02) 設定當前節點的內容為null 147 // (03) 設定後一個節點為“新的當前節點” 148 while (e != header) { 149 Entry<E> next = e.next; 150 e.next = e.previous = null; 151 e.element = null; 152 e = next; 153 } 154 header.next = header.previous = header; 155 // 設定大小為0 156 size = 0; 157 modCount++; 158 } 159 160 // 返回LinkedList指定位置的元素 161 public E get(int index) { 162 return entry(index).element; 163 } 164 165 // 設定index位置對應的節點的值為element 166 public E set(int index, E element) { 167 Entry<E> e = entry(index); 168 E oldVal = e.element; 169 e.element = element; 170 return oldVal; 171 } 172 173 // 在index前新增節點,且節點的值為element 174 public void add(int index, E element) { 175 addBefore(element, (index==size ? header : entry(index))); 176 } 177 178 // 刪除index位置的節點 179 public E remove(int index) { 180 return remove(entry(index)); 181 } 182 183 // 獲取雙向連結串列中指定位置的節點 184 private Entry<E> entry(int index) { 185 if (index < 0 || index >= size) 186 throw new IndexOutOfBoundsException("Index: "+index+ 187 ", Size: "+size); 188 Entry<E> e = header; 189 // 獲取index處的節點。 190 // 若index < 雙向連結串列長度的1/2,則從前先後查詢; 191 // 否則,從後向前查詢。 192 if (index < (size >> 1)) { 193 for (int i = 0; i <= index; i++) 194 e = e.next; 195 } else { 196 for (int i = size; i > index; i--) 197 e = e.previous; 198 } 199 return e; 200 } 201 202 // 從前向後查詢,返回“值為物件(o)的節點對應的索引” 203 // 不存在就返回-1 204 public int indexOf(Object o) { 205 int index = 0; 206 if (o==null) { 207 for (Entry e = header.next; e != header; e = e.next) { 208 if (e.element==null) 209 return index; 210 index++; 211 } 212 } else { 213 for (Entry e = header.next; e != header; e = e.next) { 214 if (o.equals(e.element)) 215 return index; 216 index++; 217 } 218 } 219 return -1; 220 } 221 222 // 從後向前查詢,返回“值為物件(o)的節點對應的索引” 223 // 不存在就返回-1 224 public int lastIndexOf(Object o) { 225 int index = size; 226 if (o==null) { 227 for (Entry e = header.previous; e != header; e = e.previous) { 228 index--; 229 if (e.element==null) 230 return index; 231 } 232 } else { 233 for (Entry e = header.previous; e != header; e = e.previous) { 234 index--; 235 if (o.equals(e.element)) 236 return index; 237 } 238 } 239 return -1; 240 } 241 242 // 返回第一個節點 243 // 若LinkedList的大小為0,則返回null 244 public E peek() { 245 if (size==0) 246 return null; 247 return getFirst(); 248 } 249 250 // 返回第一個節點 251 // 若LinkedList的大小為0,則丟擲異常 252 public E element() { 253 return getFirst(); 254 } 255 256 // 刪除並返回第一個節點 257 // 若LinkedList的大小為0,則返回null 258 public E poll() { 259 if (size==0) 260 return null; 261 return removeFirst(); 262 } 263 264 // 將e新增雙向連結串列末尾 265 public boolean offer(E e) { 266 return add(e); 267 } 268 269 // 將e新增雙向連結串列開頭 270 public boolean offerFirst(E e) { 271 addFirst(e); 272 return true; 273 } 274 275 // 將e新增雙向連結串列末尾 276 public boolean offerLast(E e) { 277 addLast(e); 278 return true; 279 } 280 281 // 返回第一個節點 282 // 若LinkedList的大小為0,則返回null 283 public E peekFirst() { 284 if (size==0) 285 return null; 286 return getFirst(); 287 } 288 289 // 返回最後一個節點 290 // 若LinkedList的大小為0,則返回null 291 public E peekLast() { 292 if (size==0) 293 return null; 294 return getLast(); 295 } 296 297 // 刪除並返回第一個節點 298 // 若LinkedList的大小為0,則返回null 299 public E pollFirst() { 300 if (size==0) 301 return null; 302 return removeFirst(); 303 } 304 305 // 刪除並返回最後一個節點 306 // 若LinkedList的大小為0,則返回null 307 public E pollLast() { 308 if (size==0) 309 return null; 310 return removeLast(); 311 } 312 313 // 將e插入到雙向連結串列開頭 314 public void push(E e) { 315 addFirst(e); 316 } 317 318 // 刪除並返回第一個節點 319 public E pop() { 320 return removeFirst(); 321 } 322 323 // 從LinkedList開始向後查詢,刪除第一個值為元素(o)的節點 324 // 從連結串列開始查詢,如存在節點的值為元素(o)的節點,則刪除該節點 325 public boolean removeFirstOccurrence(Object o) { 326 return remove(o); 327 } 328 329 // 從LinkedList末尾向前查詢,刪除第一個值為元素(o)的節點 330 // 從連結串列開始查詢,如存在節點的值為元素(o)的節點,則刪除該節點 331 public boolean removeLastOccurrence(Object o) { 332 if (o==null) { 333 for (Entry<E> e = header.previous; e != header; e = e.previous) { 334 if (e.element==null) { 335 remove(e); 336 return true; 337 } 338 } 339 } else { 340 for (Entry<E> e = header.previous; e != header; e = e.previous) { 341 if (o.equals(e.element)) { 342 remove(e); 343 return true; 344 } 345 } 346 } 347 return false; 348 } 349 350 // 返回“index到末尾的全部節點”對應的ListIterator物件(List迭代器) 351 public ListIterator<E> listIterator(int index) { 352 return new ListItr(index); 353 } 354 355 // List迭代器 356 private class ListItr implements ListIterator<E> { 357 // 上一次返回的節點 358 private Entry<E> lastReturned = header; 359 // 下一個節點 360 private Entry<E> next; 361 // 下一個節點對應的索引值 362 private int nextIndex; 363 // 期望的改變計數。用來實現fail-fast機制。 364 private int expectedModCount = modCount; 365 366 // 建構函式。 367 // 從index位置開始進行迭代 368 ListItr(int index) { 369 // index的有效性處理 370 if (index < 0 || index > size) 371 throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size); 372 // 若 “index 小於 ‘雙向連結串列長度的一半’”,則從第一個元素開始往後查詢; 373 // 否則,從最後一個元素往前查詢。 374 if (index < (size >> 1)) { 375 next = header.next; 376 for (nextIndex=0; nextIndex<index; nextIndex++) 377 next = next.next; 378 } else { 379 next = header; 380 for (nextIndex=size; nextIndex>index; nextIndex--) 381 next = next.previous; 382 } 383 } 384 385 // 是否存在下一個元素 386 public boolean hasNext() { 387 // 通過元素索引是否等於“雙向連結串列大小”來判斷是否達到最後。 388 return nextIndex != size; 389 } 390 391 // 獲取下一個元素 392 public E next() { 393 checkForComodification(); 394 if (nextIndex == size) 395 throw new NoSuchElementException(); 396 397 lastReturned = next; 398 // next指向連結串列的下一個元素 399 next = next.next; 400 nextIndex++; 401 return lastReturned.element; 402 } 403 404 // 是否存在上一個元素 405 public boolean hasPrevious() { 406 // 通過元素索引是否等於0,來判斷是否達到開頭。 407 return nextIndex != 0; 408 } 409 410 // 獲取上一個元素 411 public E previous() { 412 if (nextIndex == 0) 413 throw new NoSuchElementException(); 414 415 // next指向連結串列的上一個元素 416 lastReturned = next = next.previous; 417 nextIndex--; 418 checkForComodification(); 419 return lastReturned.element; 420 } 421 422 // 獲取下一個元素的索引 423 public int nextIndex() { 424 return nextIndex; 425 } 426 427 // 獲取上一個元素的索引 428 public int previousIndex() { 429 return nextIndex-1; 430 } 431 432 // 刪除當前元素。 433 // 刪除雙向連結串列中的當前節點 434 public void remove() { 435 checkForComodification(); 436 Entry<E> lastNext = lastReturned.next; 437 try { 438 LinkedList.this.remove(lastReturned); 439 } catch (NoSuchElementException e) { 440 throw new IllegalStateException(); 441 } 442 if (next==lastReturned) 443 next = lastNext; 444 else 445 nextIndex--; 446 lastReturned = header; 447 expectedModCount++; 448 } 449 450 // 設定當前節點為e 451 public void set(E e) { 452 if (lastReturned == header) 453 throw new IllegalStateException(); 454 checkForComodification(); 455 lastReturned.element = e; 456 } 457 458 // 將e新增到當前節點的前面 459 public void add(E e) { 460 checkForComodification(); 461 lastReturned = header; 462 addBefore(e, next); 463 nextIndex++; 464 expectedModCount++; 465 } 466 467 // 判斷 “modCount和expectedModCount是否相等”,依次來實現fail-fast機制。 468 final void checkForComodification() { 469 if (modCount != expectedModCount) 470 throw new ConcurrentModificationException(); 471 } 472 } 473 474 // 雙向連結串列的節點所對應的資料結構。 475 // 包含3部分:上一節點,下一節點,當前節點值。 476 private static class Entry<E> { 477 // 當前節點所包含的值 478 E element; 479 // 下一個節點 480 Entry<E> next; 481 // 上一個節點 482 Entry<E> previous; 483 484 /** 485 * 連結串列節點的建構函式。 486 * 引數說明: 487 * element —— 節點所包含的資料

相關推薦

Java 集合系列05 LinkedList詳細介紹(原始碼解析)使用示例

1 package java.util; 2 3 public class LinkedList<E> 4 extends AbstractSequentialList<E> 5 implements List<E>, De

Java 集合系列03 ArrayList詳細介紹(原始碼解析)使用示例

概要 上一章,我們學習了Collection的架構。這一章開始,我們對Collection的具體實現類進行講解;首先,講解List,而List中ArrayList又最為常用。因此,本章我們講解ArrayList。先對ArrayList有個整體認識,再學習它的原始

Java 集合系列10 HashMap詳細介紹(原始碼解析)使用示例

概要 這一章,我們對HashMap進行學習。 我們先對HashMap有個整體認識,然後再學習它的原始碼,最後再通過例項來學會使用HashMap。內容包括: 第1部分 HashMap介紹 第2部分 HashMap資料結構 第3部分 HashMap原始碼解析(基於J

Java 集合系列13 WeakHashMap詳細介紹(原始碼解析)使用示例

1 package java.util; 2 import java.lang.ref.WeakReference; 3 import java.lang.ref.ReferenceQueue; 4 5 public class WeakHashMap<K,V>

Java 集合系列17 TreeSet詳細介紹(原始碼解析)使用示例

1 package java.util; 2 3 public class TreeSet<E> extends AbstractSet<E> 4 implements NavigableSet<E>, Cloneable, java.i

Java 集合系列11 Hashtable詳細介紹(原始碼解析)使用示例

1 package java.util; 2 import java.io.*; 3 4 public class Hashtable<K,V> 5 extends Dictionary<K,V> 6 implements Map

Java 集合系列16 HashSet詳細介紹(原始碼解析)使用示例

1 package java.util; 2 3 public class HashSet<E> 4 extends AbstractSet<E> 5 implements Set<E>, Cloneable, java.i

Java 集合系列07 Stack詳細介紹(原始碼解析)使用示例

1 import java.util.Stack; 2 import java.util.Iterator; 3 import java.util.List; 4 5 /** 6 * @desc Stack的測試程式。測試常用API的用法 7 * 8 * @autho

Java 集合系列12 TreeMap詳細介紹(原始碼解析)使用示例

1 package java.util; 2 3 public class TreeMap<K,V> 4 extends AbstractMap<K,V> 5 implements NavigableMap<K,V>, Clone

Java 集合系列06 Vector詳細介紹(原始碼解析)使用示例

1 package java.util; 2 3 public class Vector<E> 4 extends AbstractList<E> 5 implements List<E>, RandomAccess, Cl

Java 集合系列03 ArrayList詳細介紹

ArrayList做為List介面中最常用的實現類,必須掌握。 一、ArrayList簡介 與Java中的陣列相比ArrayList的容量可以動態增加。它繼承與AbstractList,實現了List, RandomAccess, Cloneable, java.io.Serializable 和Vec

Java 集合系列13 WeakHashMap詳細介紹(源碼解析)使用示例

ted 常量 虛擬機 分享圖片 deep app default maximum obj 概要這一章,我們對WeakHashMap進行學習。我們先對WeakHashMap有個整體認識,然後再學習它的源碼,最後再通過實例來學會使用WeakHashMap。第1部分 WeakHa

Java 集合系列12 TreeMap詳細介紹(源碼解析)使用示例

叠代器 opera java port fail 繼承關系 關於 就是 開始 第1部分 TreeMap介紹TreeMap 簡介 TreeMap 是一個有序的key-value集合,它是通過紅黑樹實現的。TreeMap 繼承於AbstractMap,所以它是一個Map,即一個

java集合系列——SetHashSetTreeSet介紹(十)

最大 ... gpo 鏈表 key 同步 中大 nds set接口 一.Set的簡介Set是一個不包含重復元素的 collection。更確切地講,set 不包含滿足 e1.equals(e2) 的元素。對 e1 和 e2,並且最多包含一個為 null 的元素。 Set的類

Java 集合系列08 List總結(LinkedList, ArrayList等使用場景效能分析)

1 import java.util.*; 2 import java.lang.Class; 3 4 /* 5 * @desc 對比ArrayList和LinkedList的插入、隨機讀取效率、刪除的效率 6 * 7 * @author skywang 8 */ 9 public

java集合系列——MapTreeMap介紹(九)

一.TreeMap的簡介 TreeMap是一個有序的key-value集合,基於紅黑樹(Red-Black tree)的 NavigableMap實現。該對映根據其鍵的自然順序進行排序,或者根據建立對映時提供的 Comparator進行排序,具體取決於使用的構

Java 集合系列08 List總結(LinkedList, ArrayList等使用場景性能分

index索引 不同之處 知識點 給未來的自己 zab 默認 性能調優 程序 代碼 第1部分 List概括 先回顧一下List的框架圖 (01) List 是一個接口,它繼承於Collection的接口。它代表著有序的隊列。 (02) AbstractList 是一個抽象類

Java 集合系列02 Collection架構(JDK1.6.0_45)

首先,我們對Collection進行說明。下面先看看Collection的一些框架類的關係圖: Collection是一個介面,它主要的兩個分支是:List 和 Set。 List和Set都是介面,它們繼承於Collection。List是有序的佇列,List中可以

Java 集合系列01 總體框架 (依賴JDK1.6.0_45)

Java集合是java提供的工具包,包含了常用的資料結構:集合、連結串列、佇列、棧、陣列、對映等。Java集合工具包位置是java.util.* Java集合主要可以劃分為4個部分:List列表、Set集合、Map對映、工具類(Iterator迭代器、Enumeration列舉類、Arrays和Co

4.Java集合框架剖析 LinkedList原始碼剖析

1 package java.util; 2 3 import java.util.function.Consumer; 4 5 //LinkedList基於連結串列實現 6 //實現了List、Deque、Cloneable、Serializable介面 7 publi