CS-Notes/notes/Java 容器.md

362 lines
10 KiB
Markdown
Raw Normal View History

2018-02-21 23:28:33 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:54 +08:00
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
2018-02-21 23:28:33 +08:00
* [1. List](#1-list)
* [2. Set](#2-set)
* [3. Queue](#3-queue)
* [4. Map](#4-map)
2018-02-22 14:47:54 +08:00
* [5. Java 1.0/1.1 <20><><EFBFBD><EFBFBD>](#5-java-1011-<2D><><EFBFBD><EFBFBD>)
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ](#<23><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ģʽ)
* [1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ](#1-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ)
* [2. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ](#2-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ)
* [ɢ<EFBFBD><EFBFBD>](<><C9A2>)
* [Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](<><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
2018-02-21 23:28:33 +08:00
* [1. ArraList](#1-arralist)
2018-02-22 14:47:54 +08:00
* [2. Vector <20><> Stack](#2-vector-<2D><>-stack)
2018-02-21 23:28:33 +08:00
* [3. LinkedList](#3-linkedlist)
* [4. TreeMap](#4-treemap)
* [5. HashMap](#5-hashmap)
* [6. LinkedHashMap](#6-linkedhashmap)
* [7. ConcurrentHashMap](#7-concurrenthashmap)
2018-02-22 14:47:54 +08:00
* [<EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>)
2018-02-21 23:28:33 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ebf03f56-f957-4435-9f8f-0f605661484d.jpg)
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> Collection <20><> Map <20><><EFBFBD>֣<EFBFBD>Collection <20>ְ<EFBFBD><D6B0><EFBFBD><EFBFBD><EFBFBD> List<73><74>Set <20>Լ<EFBFBD> Queue<75><65>
2018-02-21 23:28:33 +08:00
## 1. List
2018-02-22 14:47:54 +08:00
- ArrayList<73><74><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- LinkedList<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>ֻ<EFBFBD><D6BB>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF>Կ<EFBFBD><D4BF>ٵ<EFBFBD><D9B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>Ԫ<EFBFBD>ء<EFBFBD><D8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>LinkedList <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>˫<EFBFBD>˶<EFBFBD><CBB6>С<EFBFBD>
2018-02-21 23:28:33 +08:00
## 2. Set
2018-02-22 14:47:54 +08:00
- HashSet<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash ʵ<>֣<EFBFBD>֧<EFBFBD>ֿ<EFBFBD><D6BF>ٲ<EFBFBD><D9B2>ң<EFBFBD><D2A3><EFBFBD><EFBFBD><EFBFBD>ʧȥ<CAA7><C8A5><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- TreeSet<65><74><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD><DABA><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣬵<EFBFBD><F2A3ACB5>Dz<EFBFBD><C7B2><EFBFBD>Ч<EFBFBD>ʲ<EFBFBD><CAB2><EFBFBD> HashSet<65><74>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- LinkedListHashSet<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HashSet <20>IJ<EFBFBD><C4B2><EFBFBD>Ч<EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD>ڲ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><CEAC>Ԫ<EFBFBD>صIJ<D8B5><C4B2><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾<EFBFBD><CBBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԡ<EFBFBD>
2018-02-21 23:28:33 +08:00
## 3. Queue
2018-02-22 14:47:54 +08:00
ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>LinkedList <20><> PriorityQueue<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD> LinkedList ֧<><D6A7>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>PriorityQueue <20>ǻ<EFBFBD><C7BB>ڶѽṹʵ<E1B9B9>֡<EFBFBD>
2018-02-21 23:28:33 +08:00
## 4. Map
2018-02-22 14:47:54 +08:00
- HashMap<61><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Hash ʵ<><CAB5>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- LinkedHashMap<61><70>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ά<EFBFBD><CEAC>Ԫ<EFBFBD>ص<EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>˳<EFBFBD><CBB3>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ã<EFBFBD>LRU<52><55>˳<EFBFBD><CBB3>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- TreeMap<61><70><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD><DABA><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- ConcurrentHashMap<61><70><EFBFBD>̰߳<DFB3>ȫ Map<61><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6BCB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HashTable <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
## 5. Java 1.0/1.1 <20><><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ھɵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǿ<EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>ֻ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˽⡣
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- Vector<6F><72><EFBFBD><EFBFBD> ArrayList <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- HashTable<6C><65><EFBFBD><EFBFBD> HashMap <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
# <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ģʽ
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
## 1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD>Ӹ<EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> Iterator <20><><EFBFBD>󣬿<EFBFBD><F3A3ACBF><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD>ء<EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
[Java <20>еĵ<D0B5><C4B5><EFBFBD><EFBFBD><EFBFBD>ģʽ ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md#92-java-%E5%86%85%E7%BD%AE%E7%9A%84%E8%BF%AD%E4%BB%A3%E5%99%A8)
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
## 2. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
java.util.Arrays#asList() <20><><EFBFBD>԰<EFBFBD><D4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ List <20><><EFBFBD>͡<EFBFBD>
2018-02-21 23:28:33 +08:00
```java
List list = Arrays.asList(1, 2, 3);
int[] arr = {1, 2, 3};
list = Arrays.asList(arr);
```
2018-02-22 14:47:54 +08:00
# ɢ<><C9A2>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD> hasCode() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɢ<EFBFBD><C9A2>ֵ<EFBFBD><D6B5>ʹ<EFBFBD>õ<EFBFBD><C3B5>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><D6B7>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD> equals() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ȵģ<C8B5><C4A3><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɢ<EFBFBD><C9A2>ֵһ<D6B5><D2BB>Ҫ<EFBFBD><D2AA>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɢ<EFBFBD><C9A2>ֵ<EFBFBD><D6B5>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
1. <20>Է<EFBFBD><D4B7><EFBFBD>
2. <20>Գ<EFBFBD><D4B3><EFBFBD>
3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
4. һ<><D2BB><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD>ε<EFBFBD><CEB5><EFBFBD> x.equals(y)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
5. <20><><EFBFBD>κβ<CEBA><CEB2><EFBFBD> null <20>Ķ<EFBFBD><C4B6><EFBFBD> x <20><><EFBFBD><EFBFBD> x.equals(nul) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ false
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
# Դ<><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD> [ <20>㷨 - <20><><EFBFBD><EFBFBD> ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E7%AE%97%E6%B3%95.md#%E7%AC%AC%E4%B8%89%E7%AB%A0-%E6%9F%A5%E6%89%BE) <20><><EFBFBD>֣<EFBFBD><D6A3>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>кܴ<D0BA><DCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>[OpenJDK 1.7](http://download.java.net/openjdk/jdk7)
2018-02-21 23:28:33 +08:00
## 1. ArraList
[ArraList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/ArrayList.java)
2018-02-22 14:47:54 +08:00
ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> RandomAccess <20>ӿڣ<D3BF><DAA3><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>Ϊ ArrayList <20>ǻ<EFBFBD><C7BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵġ<D6B5>
2018-02-21 23:28:33 +08:00
```java
2018-02-21 23:29:42 +08:00
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
2018-02-21 23:28:33 +08:00
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> transient <20><><EFBFBD>Σ<EFBFBD><CEA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>һ<E9B2BB><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ö<EFBFBD>ռ<EFBFBD><D5BC>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>û<EFBFBD><C3BB>Ҫȫ<D2AA><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>д writeObject() <20><> readObject()<29><>
2018-02-21 23:28:33 +08:00
```java
private transient Object[] elementData;
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϴ<EFBFBD>СΪ 10
2018-02-21 23:28:33 +08:00
```java
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
this.elementData = new Object[initialCapacity];
}
public ArrayList() {
this(10);
}
```
2018-02-22 14:47:54 +08:00
ɾ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> System.arraycopy() <20><>Ԫ<EFBFBD>ؽ<EFBFBD><D8BD>и<EFBFBD><D0B8>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD><C9B1>ܸߣ<DCB8><DFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>ʱ<EFBFBD><CAB1>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>ٸ<EFBFBD><D9B8>Ʋ<EFBFBD><C6B2><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
```java
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ʱʹ<EFBFBD><EFBFBD> ensureCapacity() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B9BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1.5 <20><><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
modCount <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼ ArrayList <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊÿ<CEAA><C3BF><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD> add() <20><> addAll() ʱ<><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> ensureCapacity()<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD> ensureCapacity() <20>ж<EFBFBD> modCount <20><><EFBFBD><EFBFBD><EFBFBD>޸ġ<DEB8>
2018-02-21 23:28:33 +08:00
```java
public void ensureCapacity(int minCapacity) {
if (minCapacity > 0)
ensureCapacityInternal(minCapacity);
}
private void ensureCapacityInternal(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
```
2018-02-22 14:47:54 +08:00
<EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȳ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ƚϲ<EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD> modCount <20>Ƿ<EFBFBD><C7B7>ı<C4B1><E4A3AC><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>׳<EFBFBD> ConcurrentModificationException<6F><6E>
2018-02-21 23:28:33 +08:00
```java
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out array length
s.writeInt(elementData.length);
// Write out all elements in the proper order.
for (int i=0; i<size; i++)
s.writeObject(elementData[i]);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
```
2018-02-22 14:47:54 +08:00
**<2A><> Vector <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
1. Vector <20><> ArrayList <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>ͬ<EFBFBD>ģ<EFBFBD>Ψһ<CEA8><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Vector <20><>ͬ<EFBFBD><CDAC><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD>˿<EFBFBD><CBBF><EFBFBD><EFBFBD>ͱ<EFBFBD> ArrayList Ҫ<>󣬷<EFBFBD><F3A3ACB7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> ArrayList <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Vector<6F><72><EFBFBD><EFBFBD>Ϊͬ<CEAA><CDAC><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ɳ<EFBFBD><C9B3><EFBFBD>Ա<EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
2. Vector ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1> 2 <20><><EFBFBD>ռ<D5BC><E4A3AC> ArrayList <20><> 1.5 <20><><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
Ϊ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>̰߳<EFBFBD>ȫ<EFBFBD><EFBFBD> ArrayList<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> Collections.synchronizedList(new ArrayList<>()); <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB> ArrayList<73><74>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> concurrent <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD> CopyOnWriteArrayList <20>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
**<2A><> LinkedList <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
1. ArrayList <20><><EFBFBD>ڶ<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>LinkedList <20><><EFBFBD><EFBFBD>˫<EFBFBD><CBAB>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>
2. ArrayList ֧<><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>LinkedList <20><>֧<EFBFBD>֣<EFBFBD>
3. LinkedList <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>Ԫ<EFBFBD>ظ<EFBFBD><D8B8>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
## 2. Vector <20><> Stack
2018-02-21 23:28:33 +08:00
[Vector.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/Vector.java)
## 3. LinkedList
[LinkedList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/LinkedList.java)
## 4. TreeMap
[TreeMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/TreeMap.java)
## 5. HashMap
[HashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
2018-02-22 14:47:54 +08:00
ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
Ĭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> capacity Ϊ 16<31><36><EFBFBD><EFBFBD>Ҫע<D2AA><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뱣֤Ϊ 2 <20>Ĵη<C4B4><CEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Entry[] table <20><><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ȣ<EFBFBD>size <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
threshold <20><EFBFBD><E6B6A8>һ<EFBFBD><D2BB> size <20><><EFBFBD>ٽ<EFBFBD>ֵ<EFBFBD><D6B5>size <20><><EFBFBD><EFBFBD>С<EFBFBD><D0A1> threshold<6C><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>ڣ<EFBFBD><DAA3>ͱ<EFBFBD><CDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
threshold = capacity * load_factor<6F><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD> load_factor Ϊ table <20><><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD>ʹ<EFBFBD>õı<C3B5><C4B1><EFBFBD><EFBFBD><EFBFBD>load_factor <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¾۴صij<D8B5><C4B3>֣<EFBFBD><D6A3>Ӷ<EFBFBD>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD>ѯ<EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼǡ<CABC>
2018-02-21 23:28:33 +08:00
```java
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final int MAXIMUM_CAPACITY = 1 << 30;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
transient Entry[] table;
transient int size;
int threshold;
final float loadFactor;
transient int modCount;
```
2018-02-22 14:47:54 +08:00
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ش<EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> capacity Ϊԭ<CEAA><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
```java
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
```
2018-02-22 14:47:54 +08:00
Entry <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾһ<CABE><D2BB><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD>е<EFBFBD> next ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>ʱ<EFBFBD><CAB1>ʹ<EFBFBD>á<EFBFBD>
2018-02-21 23:28:33 +08:00
```java
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
}
```
2018-02-22 14:47:54 +08:00
get() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ֳ<EFBFBD><D6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>key Ϊ null <20><> <20><>Ϊ null<6C><6C><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>Կ<EFBFBD><D4BF><EFBFBD> HashMap <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> null <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
```java
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}
```
2018-02-22 14:47:54 +08:00
put() <20><><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> key <20>Ƿ<EFBFBD>Ϊ null <20><><EFBFBD><EFBFBD>ͬ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫע<D2AA><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB> key Ϊ null <20>ļ<EFBFBD>ֵ<EFBFBD>ԣ<EFBFBD><D4A3>²<EFBFBD><C2B2><EFBFBD>һ<EFBFBD><D2BB> key Ϊ null <20>ļ<EFBFBD>ֵ<EFBFBD><D6B5>ʱĬ<CAB1><C4AC><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 λ<>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ null <20><><EFBFBD>ܼ<EFBFBD><DCBC><EFBFBD> hash ֵ<><D6B5>Ҳ<EFBFBD><D2B2><EFBFBD>޷<EFBFBD>֪<EFBFBD><D6AA>Ӧ<EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϡ<EFBFBD>
2018-02-21 23:28:33 +08:00
```java
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
```
```java
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
```
## 6. LinkedHashMap
[LinkedHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
## 7. ConcurrentHashMap
[ConcurrentHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
2018-02-22 14:47:54 +08:00
[ ̽<><CCBD> ConcurrentHashMap <20>߲<EFBFBD><DFB2><EFBFBD><EFBFBD>Ե<EFBFBD>ʵ<EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD> ](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/)
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
# <20>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:54 +08:00
- Java <20><><EFBFBD><EFBFBD>˼<EFBFBD><CBBC>