| 集合 | 底層結構 | 擴容機制 | 關鍵源碼點 |
| ArrayList | Object[] elementData | 1.5 倍 (newCap = oldCap + oldCap>>1) | grow(), System.arraycopy, modCount |
| LinkedList | 雙向鏈表 Node | 無(按需分配節點) | linkFirst, linkLast, unlink, modCount |
| HashMap | Node[] table + 鏈表/紅黑樹 | 2 倍 (cap << 1),負載因子 0.75 | putVal, resize, treeifyBin, hash(key) |
| HashSet | HashMap (PRESENT) | 同 HashMap | map.put(e, PRESENT) |
| TreeMap/Set | 紅黑樹 Entry | 無(樹自平衡) | put, get, rotateLeft/Right, fixAfterInsertion |
| ConcurrentHashMap | JDK7: Segment[] + HashEntry; JDK8: Node[] + CAS + synchronized | JDK8: 並發擴容 transfer | putVal, addCount, helpTransfer, sizeCtl |
六、常見坑點與最佳實踐
- ArrayList 初始容量:預知大小用
new ArrayList<>(n) 避免多次擴容拷貝。
- 遍歷中修改:
for (Iterator it = list.iterator(); it.hasNext(); ) { if (cond) it.remove(); },不可直接 list.remove() 拋 ConcurrentModificationException。
- HashMap 多線程:JDK7 死循環(鏈表倒置),JDK8 數據丟失,**嚴禁多線程共享**,用 ConcurrentHashMap。
- equals/hashCode 契約:作為 Key/Set 元素的對象必須重寫,相等對象 hashCode 必相等。
- 泛型擦除:運行期 List 實為 List
- 不可變集合:JDK9+
List.of(), Set.of(), Map.of() 創建不可變集合,線程安全、內存優化。
七、集合框架圖解(文字版思維導圖)
Collection
├─ List → ArrayList, LinkedList, Vector, Stack
├─ Set → HashSet, LinkedHashSet, TreeSet
└─ Queue → PriorityQueue, ArrayDeque, BlockingQueue→ArrayBlockingQueue,LinkedBlockingQueue
Map
├─ HashMap, LinkedHashMap, TreeMap, Hashtable
├─ ConcurrentHashMap, ConcurrentSkipListMap
└─ WeakHashMap, IdentityHashMap, EnumMap

- 上一篇文章: Java Web 學習總結(四):HTTP 協議核心詳解——請求/響應結構、狀態碼、消息頭與 Servlet 實戰控制瀏覽器行為
- 下一篇文章: Spring MVC 學習資料整理:@RequestBody/@ResponseBody 實現 Java 對象與 XML/JSON 自動轉換
Copyright ©
程式碼之家 All Rights Reserved