auto commit
This commit is contained in:
360
notes/Java 容器.md
Normal file
360
notes/Java 容器.md
Normal file
@ -0,0 +1,360 @@
|
||||
<!-- GFM-TOC -->
|
||||
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
|
||||
* [1. List](#1-list)
|
||||
* [2. Set](#2-set)
|
||||
* [3. Queue](#3-queue)
|
||||
* [4. Map](#4-map)
|
||||
* [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>)
|
||||
* [1. ArraList](#1-arralist)
|
||||
* [2. Vector <20><> Stack](#2-vector-<2D><>-stack)
|
||||
* [3. LinkedList](#3-linkedlist)
|
||||
* [4. TreeMap](#4-treemap)
|
||||
* [5. HashMap](#5-hashmap)
|
||||
* [6. LinkedHashMap](#6-linkedhashmap)
|
||||
* [7. ConcurrentHashMap](#7-concurrenthashmap)
|
||||
* [<EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
# <20><><EFBFBD><EFBFBD>
|
||||
|
||||

|
||||
|
||||
<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>
|
||||
|
||||
## 1. List
|
||||
|
||||
- ArrayList<73><74>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD>鷽<EFBFBD><E9B7BD><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>
|
||||
|
||||
- LinkedList<73><74>ʹ<EFBFBD><CAB9><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><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>
|
||||
|
||||
## 2. Set
|
||||
|
||||
- HashSet<65><74>ʹ<EFBFBD><CAB9> Hash ʵ<>֣<EFBFBD>֧<EFBFBD>ֿ<EFBFBD><D6BF>ٲ<EFBFBD><D9B2>ң<EFBFBD><D2A3><EFBFBD><EFBFBD><EFBFBD>ʧȥ<CAA7><C8A5><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>
|
||||
|
||||
- TreeSet<65><74>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F2A3ACB5>Dz<EFBFBD><C7B2><EFBFBD>Ч<EFBFBD>ʲ<EFBFBD><CAB2><EFBFBD> HashSet<65><74>
|
||||
|
||||
- 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>
|
||||
|
||||
## 3. Queue
|
||||
|
||||
ֻ<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>
|
||||
|
||||
## 4. Map
|
||||
|
||||
- HashMap<61><70>ʹ<EFBFBD><CAB9> Hash ʵ<><CAB5>
|
||||
|
||||
- LinkedHashMap<61><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><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>
|
||||
|
||||
- TreeMap<61><70><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD><DABA><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
|
||||
- ConcurrentHashMap<61><70><EFBFBD>̰߳<DFB3>ȫ Map<61><70><EFBFBD><EFBFBD><EFBFBD>漰ͬ<E6BCB0><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
## 5. Java 1.0/1.1 <20><><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><EFBFBD>˽⡣
|
||||
|
||||
- Vector<6F><72><EFBFBD><EFBFBD> ArrayList <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB>
|
||||
|
||||
- HashTable<6C><65><EFBFBD><EFBFBD> HashMap <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB>
|
||||
|
||||
# <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
||||
|
||||
## 1. <20><><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> 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>
|
||||
|
||||
[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)
|
||||
|
||||
## 2. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
||||
|
||||
java.util.Arrays#asList() <20><><EFBFBD><EFBFBD><D4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ List <20><><EFBFBD>͡<EFBFBD>
|
||||
|
||||
```java
|
||||
List list = Arrays.asList(1, 2, 3);
|
||||
int[] arr = {1, 2, 3};
|
||||
list = Arrays.asList(arr);
|
||||
```
|
||||
|
||||
# ɢ<><C9A2>
|
||||
|
||||
ʹ<EFBFBD><EFBFBD> hasCode() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɢ<EFBFBD><C9A2>ֵ<EFBFBD><D6B5>ʹ<EFBFBD>õ<EFBFBD><C3B5>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><D6B7>
|
||||
|
||||
<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>
|
||||
|
||||
<EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>
|
||||
|
||||
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
|
||||
|
||||
# Դ<><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
<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>
|
||||
|
||||
Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>[OpenJDK 1.7](http://download.java.net/openjdk/jdk7)
|
||||
|
||||
## 1. ArraList
|
||||
|
||||
[ArraList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/ArrayList.java)
|
||||
|
||||
ʵ<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>
|
||||
|
||||
```java
|
||||
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
|
||||
```
|
||||
|
||||
<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><>
|
||||
|
||||
```java
|
||||
private transient Object[] elementData;
|
||||
```
|
||||
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϴ<EFBFBD>СΪ 10
|
||||
|
||||
```java
|
||||
public ArrayList(int initialCapacity) {
|
||||
super();
|
||||
if (initialCapacity < 0)
|
||||
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
|
||||
this.elementData = new Object[initialCapacity];
|
||||
}
|
||||
|
||||
public ArrayList() {
|
||||
this(10);
|
||||
}
|
||||
```
|
||||
|
||||
ɾ<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>
|
||||
|
||||
```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;
|
||||
}
|
||||
```
|
||||
|
||||
<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>
|
||||
|
||||
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>
|
||||
|
||||
```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;
|
||||
}
|
||||
```
|
||||
|
||||
<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>
|
||||
|
||||
```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();
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
**<EFBFBD><EFBFBD> Vector <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
||||
|
||||
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>
|
||||
|
||||
Ϊ<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>ࣻ
|
||||
|
||||
**<EFBFBD><EFBFBD> LinkedList <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
||||
|
||||
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>졣
|
||||
|
||||
## 2. Vector <20><> Stack
|
||||
|
||||
[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)
|
||||
|
||||
ʹ<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 Ϊ 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>
|
||||
|
||||
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>
|
||||
|
||||
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>
|
||||
|
||||
```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;
|
||||
```
|
||||
|
||||
<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>
|
||||
|
||||
```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);
|
||||
}
|
||||
```
|
||||
|
||||
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>
|
||||
|
||||
```java
|
||||
static class Entry<K,V> implements Map.Entry<K,V> {
|
||||
final K key;
|
||||
V value;
|
||||
Entry<K,V> next;
|
||||
final int hash;
|
||||
}
|
||||
```
|
||||
|
||||
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>
|
||||
|
||||
```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;
|
||||
}
|
||||
```
|
||||
|
||||
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>
|
||||
|
||||
```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)
|
||||
|
||||
[ ̽<><CCBD> ConcurrentHashMap <20>߲<EFBFBD><DFB2><EFBFBD><EFBFBD>Ե<EFBFBD>ʵ<EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD> ](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/)
|
||||
|
||||
# <20>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
- Java <20><><EFBFBD><EFBFBD>˼<EFBFBD><CBBC>
|
152
notes/Java 集合.md
152
notes/Java 集合.md
@ -1,152 +0,0 @@
|
||||
<!-- GFM-TOC -->
|
||||
* [<EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23><><EFBFBD><EFBFBD>)
|
||||
* [1. List](#1-list)
|
||||
* [2. Set](#2-set)
|
||||
* [3. Queue](#3-queue)
|
||||
* [4. Map](#4-map)
|
||||
* [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>)
|
||||
* [1. ArraList](#1-arralist)
|
||||
* [2. LinkedList](#2-linkedlist)
|
||||
* [3. Vector](#3-vector)
|
||||
* [4. HashMap](#4-hashmap)
|
||||
* [5. LinkedHashMap](#5-linkedhashmap)
|
||||
* [6. ConcurrentHashMap](#6-concurrenthashmap)
|
||||
* [<EFBFBD>ο<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>](#<23>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>)
|
||||
<!-- GFM-TOC -->
|
||||
|
||||
# <20><><EFBFBD><EFBFBD>
|
||||
|
||||

|
||||
|
||||
<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>
|
||||
|
||||
## 1. List
|
||||
|
||||
- ArrayList<73><74>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD>鷽<EFBFBD><E9B7BD><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>
|
||||
|
||||
- LinkedList<73><74>ʹ<EFBFBD><CAB9><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><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>
|
||||
|
||||
## 2. Set
|
||||
|
||||
- HashSet<65><74>ʹ<EFBFBD><CAB9> Hash ʵ<>֣<EFBFBD>֧<EFBFBD>ֿ<EFBFBD><D6BF>ٲ<EFBFBD><D9B2>ң<EFBFBD><D2A3><EFBFBD><EFBFBD><EFBFBD>ʧȥ<CAA7><C8A5><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>
|
||||
|
||||
- TreeSet<65><74>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F2A3ACB5>Dz<EFBFBD><C7B2><EFBFBD>Ч<EFBFBD>ʲ<EFBFBD><CAB2><EFBFBD> HashSet<65><74>
|
||||
|
||||
- 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>
|
||||
|
||||
## 3. Queue
|
||||
|
||||
ֻ<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>
|
||||
|
||||
## 4. Map
|
||||
|
||||
- HashMap<61><70>ʹ<EFBFBD><CAB9> Hash ʵ<><CAB5>
|
||||
|
||||
- LinkedHashMap<61><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><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>
|
||||
|
||||
- TreeMap<61><70><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD><DABA><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
|
||||
- ConcurrentHashMap<61><70><EFBFBD>̰߳<DFB3>ȫ Map<61><70><EFBFBD><EFBFBD><EFBFBD>漰ͬ<E6BCB0><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
## 5. Java 1.0/1.1 <20><><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><EFBFBD>˽⡣
|
||||
|
||||
- Vector<6F><72><EFBFBD><EFBFBD> ArrayList <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB>
|
||||
|
||||
- HashTable<6C><65><EFBFBD><EFBFBD> HashMap <20><><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB>
|
||||
|
||||
# <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
||||
|
||||
## 1. <20><><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> 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>
|
||||
|
||||
[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)
|
||||
|
||||
## 2. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
||||
|
||||
java.util.Arrays#asList() <20><><EFBFBD><EFBFBD><D4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ List <20><><EFBFBD>͡<EFBFBD>
|
||||
|
||||
```java
|
||||
List list = Arrays.asList(1, 2, 3);
|
||||
int[] arr = {1, 2, 3};
|
||||
list = Arrays.asList(arr);
|
||||
```
|
||||
|
||||
# ɢ<><C9A2>
|
||||
|
||||
ʹ<EFBFBD><EFBFBD> hasCode() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɢ<EFBFBD><C9A2>ֵ<EFBFBD><D6B5>ʹ<EFBFBD>õ<EFBFBD><C3B5>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ<EFBFBD><D6B7>
|
||||
|
||||
<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>
|
||||
|
||||
<EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʣ<EFBFBD>
|
||||
|
||||
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
|
||||
|
||||
# Դ<><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD> [<EFBFBD>㷨-<2D><><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>
|
||||
|
||||
|
||||
Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>[OpenJDK 1.7](http://download.java.net/openjdk/jdk7)
|
||||
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD>[7u40-b43](http://grepcode.com/snapshot/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/)
|
||||
|
||||
## 1. ArraList
|
||||
|
||||
- ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
|
||||
|
||||
- <20><><EFBFBD>ж<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 10<31><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>ʱʹ<CAB1><CAB9> ensureCapacity() <20><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD>㹻<EFBFBD><E3B9BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1.5 times + 1.
|
||||
|
||||
[ArraList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/ArrayList.java)
|
||||
|
||||
|
||||
## 2. LinkedList
|
||||
|
||||
- LinkedList <20>ǻ<EFBFBD><C7BB><EFBFBD>˫<EFBFBD><CBAB>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>㲻<EFBFBD><E3B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
|
||||
|
||||

|
||||
|
||||
- <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LinkedList <20><> Entry entry(int index) <20><><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><D2BB><EFBFBD>Ż<EFBFBD><C5BB>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>棬<EFBFBD><E6A3AC>ô<EFBFBD>ʹ<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD><DABA><EFBFBD><EFBFBD>ʹӺ<CDB4><D3BA><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
[LinkedList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/LinkedList.java)
|
||||
|
||||
## 3. Vector
|
||||
|
||||
Vector <20>ĺܶ<C4BA>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>䣬<EFBFBD><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD>ġ<EFBFBD>
|
||||
|
||||
[Vector.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/Vector.java)
|
||||
|
||||
## 4. HashMap
|
||||
|
||||
- ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB>
|
||||
|
||||
[Vector.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
|
||||
|
||||
|
||||
## 5. LinkedHashMap
|
||||
|
||||
- ʹ<><CAB9>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ㣬<DAB5>Ӷ<EFBFBD>ά<EFBFBD><CEAC>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>
|
||||
- ע<><D7A2>Դ<EFBFBD><D4B4><EFBFBD>е<EFBFBD>accessOrder<65><72>־λ<D6BE><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>falseʱ<65><CAB1><EFBFBD><EFBFBD>ʾ˫<CABE><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD>ذ<EFBFBD><D8B0><EFBFBD>Entry<72><79><EFBFBD><EFBFBD>LinkedHashMap<61><70><EFBFBD>е<EFBFBD><D0B5>Ⱥ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>put<75><74>LinkedHashMap<61>е<EFBFBD>Entry<72><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Entry<72><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3>һ<EFBFBD>£<EFBFBD><C2A3><EFBFBD>Ҳ<EFBFBD><D2B2>Ĭ<EFBFBD>ϵ<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ洢˳<E6B4A2><EFBFBD><F2A3BBB5><EFBFBD>Ϊtrueʱ<65><CAB1><EFBFBD><EFBFBD>ʾ˫<CABE><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD>ذ<EFBFBD><D8B0>շ<EFBFBD><D5B7>ʵ<EFBFBD><CAB5>Ⱥ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȻEntry<72><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD>Ȼ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>put<75><74>LinkedHashMap<61>е<EFBFBD>˳<EFBFBD><EFBFBD>put<75><74>get<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD>recordAccess<73><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>put<75><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>key<65><79>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD>е<EFBFBD>Entry<72><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>recordAccess<73><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD>ж<EFBFBD>accessOrder<65>Ƿ<EFBFBD>Ϊtrue<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD><C7A3><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ʵ<EFBFBD>Entry<72><79>put<75><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Entry<72><79>get<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Entry<72><79><EFBFBD>Ƶ<EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD>key<65><79><EFBFBD><EFBFBD>ͬʱ<CDAC><CAB1>put<75><74>Entryʱ<79><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>addEntry<72><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>creatEntry<72><79><EFBFBD>÷<EFBFBD><C3B7><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>²<EFBFBD><C2B2><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ط<EFBFBD><D8B7>뵽˫<EBB5BD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7>ϲ<EFBFBD><CFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>Ϸ<EFBFBD><CFB7>ʵ<EFBFBD><CAB5>Ⱥ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ<EFBFBD><CAB1>EntryҲ<79><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʲôҲ<C3B4><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵˵LinkedHashMap<61><70><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>LRU<52>ġ<EFBFBD><C4A1><EFBFBD><EFBFBD>ȣ<EFBFBD><C8A3><EFBFBD>accessOrderΪtrueʱ<65><CAB1><EFBFBD>ŻῪ<C5BB><E1BFAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>LRU<52>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF>Կ<EFBFBD><D4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>put<75><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>get<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ᵼ<EFBFBD><E1B5BC>Ŀ<EFBFBD><C4BF>Entry<72><79>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>Entry<72><79><EFBFBD><EFBFBD><EFBFBD>˱<EFBFBD><CBB1>Ѹ<EFBFBD>Entry<72><79><EFBFBD>뵽<EFBFBD><EBB5BD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĩβ<C4A9><CEB2>get<65><74><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>recordAccess<73><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>put<75><74><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>key<65><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>Ҳ<EFBFBD><D2B2>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>recordAccess<73><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD>µ<EFBFBD>Entryʱ<79><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>createEntry<72>е<EFBFBD>addBefore<72><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>˵<EFBFBD>Entry<72><79><EFBFBD>뵽<EFBFBD><EBB5BD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA>棬<EFBFBD><E6A3AC><EFBFBD>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Entry<72><79><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ʹ<EFBFBD>õģ<C3B5><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>Entry(head<61><64><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǹ<EFBFBD>Entry)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD>Entry<72><79>
|
||||
|
||||
[LinkedHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
|
||||
|
||||
## 6. ConcurrentHashMap
|
||||
|
||||
[̽<EFBFBD><EFBFBD> ConcurrentHashMap <20>߲<EFBFBD><DFB2><EFBFBD><EFBFBD>Ե<EFBFBD>ʵ<EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/)
|
||||
|
||||
[ConcurrentHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
|
||||
|
||||
# <20>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
- Java <20><><EFBFBD><EFBFBD>˼<EFBFBD><CBBC>
|
@ -1,37 +1,3 @@
|
||||
/*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
* However, the following notice accompanied the original version of this
|
||||
* file:
|
||||
*
|
||||
* Written by Doug Lea with assistance from members of JCP JSR-166
|
||||
* Expert Group and released to the public domain, as explained at
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
package java.util.concurrent;
|
||||
import java.util.concurrent.locks.*;
|
||||
@ -41,64 +7,6 @@ import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
/**
|
||||
* A hash table supporting full concurrency of retrievals and
|
||||
* adjustable expected concurrency for updates. This class obeys the
|
||||
* same functional specification as {@link java.util.Hashtable}, and
|
||||
* includes versions of methods corresponding to each method of
|
||||
* <tt>Hashtable</tt>. However, even though all operations are
|
||||
* thread-safe, retrieval operations do <em>not</em> entail locking,
|
||||
* and there is <em>not</em> any support for locking the entire table
|
||||
* in a way that prevents all access. This class is fully
|
||||
* interoperable with <tt>Hashtable</tt> in programs that rely on its
|
||||
* thread safety but not on its synchronization details.
|
||||
*
|
||||
* <p> Retrieval operations (including <tt>get</tt>) generally do not
|
||||
* block, so may overlap with update operations (including
|
||||
* <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results
|
||||
* of the most recently <em>completed</em> update operations holding
|
||||
* upon their onset. For aggregate operations such as <tt>putAll</tt>
|
||||
* and <tt>clear</tt>, concurrent retrievals may reflect insertion or
|
||||
* removal of only some entries. Similarly, Iterators and
|
||||
* Enumerations return elements reflecting the state of the hash table
|
||||
* at some point at or since the creation of the iterator/enumeration.
|
||||
* They do <em>not</em> throw {@link ConcurrentModificationException}.
|
||||
* However, iterators are designed to be used by only one thread at a time.
|
||||
*
|
||||
* <p> The allowed concurrency among update operations is guided by
|
||||
* the optional <tt>concurrencyLevel</tt> constructor argument
|
||||
* (default <tt>16</tt>), which is used as a hint for internal sizing. The
|
||||
* table is internally partitioned to try to permit the indicated
|
||||
* number of concurrent updates without contention. Because placement
|
||||
* in hash tables is essentially random, the actual concurrency will
|
||||
* vary. Ideally, you should choose a value to accommodate as many
|
||||
* threads as will ever concurrently modify the table. Using a
|
||||
* significantly higher value than you need can waste space and time,
|
||||
* and a significantly lower value can lead to thread contention. But
|
||||
* overestimates and underestimates within an order of magnitude do
|
||||
* not usually have much noticeable impact. A value of one is
|
||||
* appropriate when it is known that only one thread will modify and
|
||||
* all others will only read. Also, resizing this or any other kind of
|
||||
* hash table is a relatively slow operation, so, when possible, it is
|
||||
* a good idea to provide estimates of expected table sizes in
|
||||
* constructors.
|
||||
*
|
||||
* <p>This class and its views and iterators implement all of the
|
||||
* <em>optional</em> methods of the {@link Map} and {@link Iterator}
|
||||
* interfaces.
|
||||
*
|
||||
* <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
|
||||
* does <em>not</em> allow <tt>null</tt> to be used as a key or value.
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @since 1.5
|
||||
* @author Doug Lea
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
*/
|
||||
public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
|
||||
implements ConcurrentMap<K, V>, Serializable {
|
||||
private static final long serialVersionUID = 7249069246763182397L;
|
||||
|
@ -1,126 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Hash table based implementation of the <tt>Map</tt> interface. This
|
||||
* implementation provides all of the optional map operations, and permits
|
||||
* <tt>null</tt> values and the <tt>null</tt> key. (The <tt>HashMap</tt>
|
||||
* class is roughly equivalent to <tt>Hashtable</tt>, except that it is
|
||||
* unsynchronized and permits nulls.) This class makes no guarantees as to
|
||||
* the order of the map; in particular, it does not guarantee that the order
|
||||
* will remain constant over time.
|
||||
*
|
||||
* <p>This implementation provides constant-time performance for the basic
|
||||
* operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
|
||||
* disperses the elements properly among the buckets. Iteration over
|
||||
* collection views requires time proportional to the "capacity" of the
|
||||
* <tt>HashMap</tt> instance (the number of buckets) plus its size (the number
|
||||
* of key-value mappings). Thus, it's very important not to set the initial
|
||||
* capacity too high (or the load factor too low) if iteration performance is
|
||||
* important.
|
||||
*
|
||||
* <p>An instance of <tt>HashMap</tt> has two parameters that affect its
|
||||
* performance: <i>initial capacity</i> and <i>load factor</i>. The
|
||||
* <i>capacity</i> is the number of buckets in the hash table, and the initial
|
||||
* capacity is simply the capacity at the time the hash table is created. The
|
||||
* <i>load factor</i> is a measure of how full the hash table is allowed to
|
||||
* get before its capacity is automatically increased. When the number of
|
||||
* entries in the hash table exceeds the product of the load factor and the
|
||||
* current capacity, the hash table is <i>rehashed</i> (that is, internal data
|
||||
* structures are rebuilt) so that the hash table has approximately twice the
|
||||
* number of buckets.
|
||||
*
|
||||
* <p>As a general rule, the default load factor (.75) offers a good tradeoff
|
||||
* between time and space costs. Higher values decrease the space overhead
|
||||
* but increase the lookup cost (reflected in most of the operations of the
|
||||
* <tt>HashMap</tt> class, including <tt>get</tt> and <tt>put</tt>). The
|
||||
* expected number of entries in the map and its load factor should be taken
|
||||
* into account when setting its initial capacity, so as to minimize the
|
||||
* number of rehash operations. If the initial capacity is greater
|
||||
* than the maximum number of entries divided by the load factor, no
|
||||
* rehash operations will ever occur.
|
||||
*
|
||||
* <p>If many mappings are to be stored in a <tt>HashMap</tt> instance,
|
||||
* creating it with a sufficiently large capacity will allow the mappings to
|
||||
* be stored more efficiently than letting it perform automatic rehashing as
|
||||
* needed to grow the table.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a hash map concurrently, and at least one of
|
||||
* the threads modifies the map structurally, it <i>must</i> be
|
||||
* synchronized externally. (A structural modification is any operation
|
||||
* that adds or deletes one or more mappings; merely changing the value
|
||||
* associated with a key that an instance already contains is not a
|
||||
* structural modification.) This is typically accomplished by
|
||||
* synchronizing on some object that naturally encapsulates the map.
|
||||
*
|
||||
* If no such object exists, the map should be "wrapped" using the
|
||||
* {@link Collections#synchronizedMap Collections.synchronizedMap}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the map:<pre>
|
||||
* Map m = Collections.synchronizedMap(new HashMap(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by all of this class's "collection view methods"
|
||||
* are <i>fail-fast</i>: if the map is structurally modified at any time after
|
||||
* the iterator is created, in any way except through the iterator's own
|
||||
* <tt>remove</tt> method, the iterator will throw a
|
||||
* {@link ConcurrentModificationException}. Thus, in the face of concurrent
|
||||
* modification, the iterator fails quickly and cleanly, rather than risking
|
||||
* arbitrary, non-deterministic behavior at an undetermined time in the
|
||||
* future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
*
|
||||
* @author Doug Lea
|
||||
* @author Josh Bloch
|
||||
* @author Arthur van Hoff
|
||||
* @author Neal Gafter
|
||||
* @see Object#hashCode()
|
||||
* @see Collection
|
||||
* @see Map
|
||||
* @see TreeMap
|
||||
* @see Hashtable
|
||||
* @since 1.2
|
||||
*/
|
||||
|
||||
public class HashMap<K,V>
|
||||
extends AbstractMap<K,V>
|
||||
implements Map<K,V>, Cloneable, Serializable
|
||||
|
@ -1,89 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* This class implements the <tt>Set</tt> interface, backed by a hash table
|
||||
* (actually a <tt>HashMap</tt> instance). It makes no guarantees as to the
|
||||
* iteration order of the set; in particular, it does not guarantee that the
|
||||
* order will remain constant over time. This class permits the <tt>null</tt>
|
||||
* element.
|
||||
*
|
||||
* <p>This class offers constant time performance for the basic operations
|
||||
* (<tt>add</tt>, <tt>remove</tt>, <tt>contains</tt> and <tt>size</tt>),
|
||||
* assuming the hash function disperses the elements properly among the
|
||||
* buckets. Iterating over this set requires time proportional to the sum of
|
||||
* the <tt>HashSet</tt> instance's size (the number of elements) plus the
|
||||
* "capacity" of the backing <tt>HashMap</tt> instance (the number of
|
||||
* buckets). Thus, it's very important not to set the initial capacity too
|
||||
* high (or the load factor too low) if iteration performance is important.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a hash set concurrently, and at least one of
|
||||
* the threads modifies the set, it <i>must</i> be synchronized externally.
|
||||
* This is typically accomplished by synchronizing on some object that
|
||||
* naturally encapsulates the set.
|
||||
*
|
||||
* If no such object exists, the set should be "wrapped" using the
|
||||
* {@link Collections#synchronizedSet Collections.synchronizedSet}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the set:<pre>
|
||||
* Set s = Collections.synchronizedSet(new HashSet(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by this class's <tt>iterator</tt> method are
|
||||
* <i>fail-fast</i>: if the set is modified at any time after the iterator is
|
||||
* created, in any way except through the iterator's own <tt>remove</tt>
|
||||
* method, the Iterator throws a {@link ConcurrentModificationException}.
|
||||
* Thus, in the face of concurrent modification, the iterator fails quickly
|
||||
* and cleanly, rather than risking arbitrary, non-deterministic behavior at
|
||||
* an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <E> the type of elements maintained by this set
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @author Neal Gafter
|
||||
* @see Collection
|
||||
* @see Set
|
||||
* @see TreeSet
|
||||
* @see HashMap
|
||||
* @since 1.2
|
||||
*/
|
||||
|
||||
public class HashSet<E>
|
||||
extends AbstractSet<E>
|
||||
implements Set<E>, Cloneable, java.io.Serializable
|
||||
|
@ -1,149 +1,7 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* <p>Hash table and linked list implementation of the <tt>Map</tt> interface,
|
||||
* with predictable iteration order. This implementation differs from
|
||||
* <tt>HashMap</tt> in that it maintains a doubly-linked list running through
|
||||
* all of its entries. This linked list defines the iteration ordering,
|
||||
* which is normally the order in which keys were inserted into the map
|
||||
* (<i>insertion-order</i>). Note that insertion order is not affected
|
||||
* if a key is <i>re-inserted</i> into the map. (A key <tt>k</tt> is
|
||||
* reinserted into a map <tt>m</tt> if <tt>m.put(k, v)</tt> is invoked when
|
||||
* <tt>m.containsKey(k)</tt> would return <tt>true</tt> immediately prior to
|
||||
* the invocation.)
|
||||
*
|
||||
* <p>This implementation spares its clients from the unspecified, generally
|
||||
* chaotic ordering provided by {@link HashMap} (and {@link Hashtable}),
|
||||
* without incurring the increased cost associated with {@link TreeMap}. It
|
||||
* can be used to produce a copy of a map that has the same order as the
|
||||
* original, regardless of the original map's implementation:
|
||||
* <pre>
|
||||
* void foo(Map m) {
|
||||
* Map copy = new LinkedHashMap(m);
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
* This technique is particularly useful if a module takes a map on input,
|
||||
* copies it, and later returns results whose order is determined by that of
|
||||
* the copy. (Clients generally appreciate having things returned in the same
|
||||
* order they were presented.)
|
||||
*
|
||||
* <p>A special {@link #LinkedHashMap(int,float,boolean) constructor} is
|
||||
* provided to create a linked hash map whose order of iteration is the order
|
||||
* in which its entries were last accessed, from least-recently accessed to
|
||||
* most-recently (<i>access-order</i>). This kind of map is well-suited to
|
||||
* building LRU caches. Invoking the <tt>put</tt> or <tt>get</tt> method
|
||||
* results in an access to the corresponding entry (assuming it exists after
|
||||
* the invocation completes). The <tt>putAll</tt> method generates one entry
|
||||
* access for each mapping in the specified map, in the order that key-value
|
||||
* mappings are provided by the specified map's entry set iterator. <i>No
|
||||
* other methods generate entry accesses.</i> In particular, operations on
|
||||
* collection-views do <i>not</i> affect the order of iteration of the backing
|
||||
* map.
|
||||
*
|
||||
* <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
|
||||
* impose a policy for removing stale mappings automatically when new mappings
|
||||
* are added to the map.
|
||||
*
|
||||
* <p>This class provides all of the optional <tt>Map</tt> operations, and
|
||||
* permits null elements. Like <tt>HashMap</tt>, it provides constant-time
|
||||
* performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
|
||||
* <tt>remove</tt>), assuming the hash function disperses elements
|
||||
* properly among the buckets. Performance is likely to be just slightly
|
||||
* below that of <tt>HashMap</tt>, due to the added expense of maintaining the
|
||||
* linked list, with one exception: Iteration over the collection-views
|
||||
* of a <tt>LinkedHashMap</tt> requires time proportional to the <i>size</i>
|
||||
* of the map, regardless of its capacity. Iteration over a <tt>HashMap</tt>
|
||||
* is likely to be more expensive, requiring time proportional to its
|
||||
* <i>capacity</i>.
|
||||
*
|
||||
* <p>A linked hash map has two parameters that affect its performance:
|
||||
* <i>initial capacity</i> and <i>load factor</i>. They are defined precisely
|
||||
* as for <tt>HashMap</tt>. Note, however, that the penalty for choosing an
|
||||
* excessively high value for initial capacity is less severe for this class
|
||||
* than for <tt>HashMap</tt>, as iteration times for this class are unaffected
|
||||
* by capacity.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a linked hash map concurrently, and at least
|
||||
* one of the threads modifies the map structurally, it <em>must</em> be
|
||||
* synchronized externally. This is typically accomplished by
|
||||
* synchronizing on some object that naturally encapsulates the map.
|
||||
*
|
||||
* If no such object exists, the map should be "wrapped" using the
|
||||
* {@link Collections#synchronizedMap Collections.synchronizedMap}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the map:<pre>
|
||||
* Map m = Collections.synchronizedMap(new LinkedHashMap(...));</pre>
|
||||
*
|
||||
* A structural modification is any operation that adds or deletes one or more
|
||||
* mappings or, in the case of access-ordered linked hash maps, affects
|
||||
* iteration order. In insertion-ordered linked hash maps, merely changing
|
||||
* the value associated with a key that is already contained in the map is not
|
||||
* a structural modification. <strong>In access-ordered linked hash maps,
|
||||
* merely querying the map with <tt>get</tt> is a structural
|
||||
* modification.</strong>)
|
||||
*
|
||||
* <p>The iterators returned by the <tt>iterator</tt> method of the collections
|
||||
* returned by all of this class's collection view methods are
|
||||
* <em>fail-fast</em>: if the map is structurally modified at any time after
|
||||
* the iterator is created, in any way except through the iterator's own
|
||||
* <tt>remove</tt> method, the iterator will throw a {@link
|
||||
* ConcurrentModificationException}. Thus, in the face of concurrent
|
||||
* modification, the iterator fails quickly and cleanly, rather than risking
|
||||
* arbitrary, non-deterministic behavior at an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @see Object#hashCode()
|
||||
* @see Collection
|
||||
* @see Map
|
||||
* @see HashMap
|
||||
* @see TreeMap
|
||||
* @see Hashtable
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class LinkedHashMap<K,V>
|
||||
extends HashMap<K,V>
|
||||
implements Map<K,V>
|
||||
|
@ -1,120 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* <p>Hash table and linked list implementation of the <tt>Set</tt> interface,
|
||||
* with predictable iteration order. This implementation differs from
|
||||
* <tt>HashSet</tt> in that it maintains a doubly-linked list running through
|
||||
* all of its entries. This linked list defines the iteration ordering,
|
||||
* which is the order in which elements were inserted into the set
|
||||
* (<i>insertion-order</i>). Note that insertion order is <i>not</i> affected
|
||||
* if an element is <i>re-inserted</i> into the set. (An element <tt>e</tt>
|
||||
* is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when
|
||||
* <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to
|
||||
* the invocation.)
|
||||
*
|
||||
* <p>This implementation spares its clients from the unspecified, generally
|
||||
* chaotic ordering provided by {@link HashSet}, without incurring the
|
||||
* increased cost associated with {@link TreeSet}. It can be used to
|
||||
* produce a copy of a set that has the same order as the original, regardless
|
||||
* of the original set's implementation:
|
||||
* <pre>
|
||||
* void foo(Set s) {
|
||||
* Set copy = new LinkedHashSet(s);
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
* This technique is particularly useful if a module takes a set on input,
|
||||
* copies it, and later returns results whose order is determined by that of
|
||||
* the copy. (Clients generally appreciate having things returned in the same
|
||||
* order they were presented.)
|
||||
*
|
||||
* <p>This class provides all of the optional <tt>Set</tt> operations, and
|
||||
* permits null elements. Like <tt>HashSet</tt>, it provides constant-time
|
||||
* performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
|
||||
* <tt>remove</tt>), assuming the hash function disperses elements
|
||||
* properly among the buckets. Performance is likely to be just slightly
|
||||
* below that of <tt>HashSet</tt>, due to the added expense of maintaining the
|
||||
* linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt>
|
||||
* requires time proportional to the <i>size</i> of the set, regardless of
|
||||
* its capacity. Iteration over a <tt>HashSet</tt> is likely to be more
|
||||
* expensive, requiring time proportional to its <i>capacity</i>.
|
||||
*
|
||||
* <p>A linked hash set has two parameters that affect its performance:
|
||||
* <i>initial capacity</i> and <i>load factor</i>. They are defined precisely
|
||||
* as for <tt>HashSet</tt>. Note, however, that the penalty for choosing an
|
||||
* excessively high value for initial capacity is less severe for this class
|
||||
* than for <tt>HashSet</tt>, as iteration times for this class are unaffected
|
||||
* by capacity.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a linked hash set concurrently, and at least
|
||||
* one of the threads modifies the set, it <em>must</em> be synchronized
|
||||
* externally. This is typically accomplished by synchronizing on some
|
||||
* object that naturally encapsulates the set.
|
||||
*
|
||||
* If no such object exists, the set should be "wrapped" using the
|
||||
* {@link Collections#synchronizedSet Collections.synchronizedSet}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the set: <pre>
|
||||
* Set s = Collections.synchronizedSet(new LinkedHashSet(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by this class's <tt>iterator</tt> method are
|
||||
* <em>fail-fast</em>: if the set is modified at any time after the iterator
|
||||
* is created, in any way except through the iterator's own <tt>remove</tt>
|
||||
* method, the iterator will throw a {@link ConcurrentModificationException}.
|
||||
* Thus, in the face of concurrent modification, the iterator fails quickly
|
||||
* and cleanly, rather than risking arbitrary, non-deterministic behavior at
|
||||
* an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <E> the type of elements maintained by this set
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @see Object#hashCode()
|
||||
* @see Collection
|
||||
* @see Set
|
||||
* @see HashSet
|
||||
* @see TreeSet
|
||||
* @see Hashtable
|
||||
* @since 1.4
|
||||
*/
|
||||
|
||||
public class LinkedHashSet<E>
|
||||
extends HashSet<E>
|
||||
implements Set<E>, Cloneable, java.io.Serializable {
|
||||
|
@ -1,83 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* Doubly-linked list implementation of the {@code List} and {@code Deque}
|
||||
* interfaces. Implements all optional list operations, and permits all
|
||||
* elements (including {@code null}).
|
||||
*
|
||||
* <p>All of the operations perform as could be expected for a doubly-linked
|
||||
* list. Operations that index into the list will traverse the list from
|
||||
* the beginning or the end, whichever is closer to the specified index.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a linked list concurrently, and at least
|
||||
* one of the threads modifies the list structurally, it <i>must</i> be
|
||||
* synchronized externally. (A structural modification is any operation
|
||||
* that adds or deletes one or more elements; merely setting the value of
|
||||
* an element is not a structural modification.) This is typically
|
||||
* accomplished by synchronizing on some object that naturally
|
||||
* encapsulates the list.
|
||||
*
|
||||
* If no such object exists, the list should be "wrapped" using the
|
||||
* {@link Collections#synchronizedList Collections.synchronizedList}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the list:<pre>
|
||||
* List list = Collections.synchronizedList(new LinkedList(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by this class's {@code iterator} and
|
||||
* {@code listIterator} methods are <i>fail-fast</i>: if the list is
|
||||
* structurally modified at any time after the iterator is created, in
|
||||
* any way except through the Iterator's own {@code remove} or
|
||||
* {@code add} methods, the iterator will throw a {@link
|
||||
* ConcurrentModificationException}. Thus, in the face of concurrent
|
||||
* modification, the iterator fails quickly and cleanly, rather than
|
||||
* risking arbitrary, non-deterministic behavior at an undetermined
|
||||
* time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @see List
|
||||
* @see ArrayList
|
||||
* @since 1.2
|
||||
* @param <E> the type of elements held in this collection
|
||||
*/
|
||||
|
||||
public class LinkedList<E>
|
||||
extends AbstractSequentialList<E>
|
||||
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
|
||||
|
@ -1,82 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* An unbounded priority {@linkplain Queue queue} based on a priority heap.
|
||||
* The elements of the priority queue are ordered according to their
|
||||
* {@linkplain Comparable natural ordering}, or by a {@link Comparator}
|
||||
* provided at queue construction time, depending on which constructor is
|
||||
* used. A priority queue does not permit {@code null} elements.
|
||||
* A priority queue relying on natural ordering also does not permit
|
||||
* insertion of non-comparable objects (doing so may result in
|
||||
* {@code ClassCastException}).
|
||||
*
|
||||
* <p>The <em>head</em> of this queue is the <em>least</em> element
|
||||
* with respect to the specified ordering. If multiple elements are
|
||||
* tied for least value, the head is one of those elements -- ties are
|
||||
* broken arbitrarily. The queue retrieval operations {@code poll},
|
||||
* {@code remove}, {@code peek}, and {@code element} access the
|
||||
* element at the head of the queue.
|
||||
*
|
||||
* <p>A priority queue is unbounded, but has an internal
|
||||
* <i>capacity</i> governing the size of an array used to store the
|
||||
* elements on the queue. It is always at least as large as the queue
|
||||
* size. As elements are added to a priority queue, its capacity
|
||||
* grows automatically. The details of the growth policy are not
|
||||
* specified.
|
||||
*
|
||||
* <p>This class and its iterator implement all of the
|
||||
* <em>optional</em> methods of the {@link Collection} and {@link
|
||||
* Iterator} interfaces. The Iterator provided in method {@link
|
||||
* #iterator()} is <em>not</em> guaranteed to traverse the elements of
|
||||
* the priority queue in any particular order. If you need ordered
|
||||
* traversal, consider using {@code Arrays.sort(pq.toArray())}.
|
||||
*
|
||||
* <p> <strong>Note that this implementation is not synchronized.</strong>
|
||||
* Multiple threads should not access a {@code PriorityQueue}
|
||||
* instance concurrently if any of the threads modifies the queue.
|
||||
* Instead, use the thread-safe {@link
|
||||
* java.util.concurrent.PriorityBlockingQueue} class.
|
||||
*
|
||||
* <p>Implementation note: this implementation provides
|
||||
* O(log(n)) time for the enqueing and dequeing methods
|
||||
* ({@code offer}, {@code poll}, {@code remove()} and {@code add});
|
||||
* linear time for the {@code remove(Object)} and {@code contains(Object)}
|
||||
* methods; and constant time for the retrieval methods
|
||||
* ({@code peek}, {@code element}, and {@code size}).
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @since 1.5
|
||||
* @author Josh Bloch, Doug Lea
|
||||
* @param <E> the type of elements held in this collection
|
||||
*/
|
||||
public class PriorityQueue<E> extends AbstractQueue<E>
|
||||
implements java.io.Serializable {
|
||||
|
||||
|
@ -1,146 +1,6 @@
|
||||
/*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
* However, the following notice accompanied the original version of this
|
||||
* file:
|
||||
*
|
||||
* Written by Doug Lea with assistance from members of JCP JSR-166
|
||||
* Expert Group and released to the public domain, as explained at
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* A collection designed for holding elements prior to processing.
|
||||
* Besides basic {@link java.util.Collection Collection} operations,
|
||||
* queues provide additional insertion, extraction, and inspection
|
||||
* operations. Each of these methods exists in two forms: one throws
|
||||
* an exception if the operation fails, the other returns a special
|
||||
* value (either <tt>null</tt> or <tt>false</tt>, depending on the
|
||||
* operation). The latter form of the insert operation is designed
|
||||
* specifically for use with capacity-restricted <tt>Queue</tt>
|
||||
* implementations; in most implementations, insert operations cannot
|
||||
* fail.
|
||||
*
|
||||
* <p>
|
||||
* <table BORDER CELLPADDING=3 CELLSPACING=1>
|
||||
* <tr>
|
||||
* <td></td>
|
||||
* <td ALIGN=CENTER><em>Throws exception</em></td>
|
||||
* <td ALIGN=CENTER><em>Returns special value</em></td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><b>Insert</b></td>
|
||||
* <td>{@link #add add(e)}</td>
|
||||
* <td>{@link #offer offer(e)}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><b>Remove</b></td>
|
||||
* <td>{@link #remove remove()}</td>
|
||||
* <td>{@link #poll poll()}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td><b>Examine</b></td>
|
||||
* <td>{@link #element element()}</td>
|
||||
* <td>{@link #peek peek()}</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* <p>Queues typically, but do not necessarily, order elements in a
|
||||
* FIFO (first-in-first-out) manner. Among the exceptions are
|
||||
* priority queues, which order elements according to a supplied
|
||||
* comparator, or the elements' natural ordering, and LIFO queues (or
|
||||
* stacks) which order the elements LIFO (last-in-first-out).
|
||||
* Whatever the ordering used, the <em>head</em> of the queue is that
|
||||
* element which would be removed by a call to {@link #remove() } or
|
||||
* {@link #poll()}. In a FIFO queue, all new elements are inserted at
|
||||
* the <em> tail</em> of the queue. Other kinds of queues may use
|
||||
* different placement rules. Every <tt>Queue</tt> implementation
|
||||
* must specify its ordering properties.
|
||||
*
|
||||
* <p>The {@link #offer offer} method inserts an element if possible,
|
||||
* otherwise returning <tt>false</tt>. This differs from the {@link
|
||||
* java.util.Collection#add Collection.add} method, which can fail to
|
||||
* add an element only by throwing an unchecked exception. The
|
||||
* <tt>offer</tt> method is designed for use when failure is a normal,
|
||||
* rather than exceptional occurrence, for example, in fixed-capacity
|
||||
* (or "bounded") queues.
|
||||
*
|
||||
* <p>The {@link #remove()} and {@link #poll()} methods remove and
|
||||
* return the head of the queue.
|
||||
* Exactly which element is removed from the queue is a
|
||||
* function of the queue's ordering policy, which differs from
|
||||
* implementation to implementation. The <tt>remove()</tt> and
|
||||
* <tt>poll()</tt> methods differ only in their behavior when the
|
||||
* queue is empty: the <tt>remove()</tt> method throws an exception,
|
||||
* while the <tt>poll()</tt> method returns <tt>null</tt>.
|
||||
*
|
||||
* <p>The {@link #element()} and {@link #peek()} methods return, but do
|
||||
* not remove, the head of the queue.
|
||||
*
|
||||
* <p>The <tt>Queue</tt> interface does not define the <i>blocking queue
|
||||
* methods</i>, which are common in concurrent programming. These methods,
|
||||
* which wait for elements to appear or for space to become available, are
|
||||
* defined in the {@link java.util.concurrent.BlockingQueue} interface, which
|
||||
* extends this interface.
|
||||
*
|
||||
* <p><tt>Queue</tt> implementations generally do not allow insertion
|
||||
* of <tt>null</tt> elements, although some implementations, such as
|
||||
* {@link LinkedList}, do not prohibit insertion of <tt>null</tt>.
|
||||
* Even in the implementations that permit it, <tt>null</tt> should
|
||||
* not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also
|
||||
* used as a special return value by the <tt>poll</tt> method to
|
||||
* indicate that the queue contains no elements.
|
||||
*
|
||||
* <p><tt>Queue</tt> implementations generally do not define
|
||||
* element-based versions of methods <tt>equals</tt> and
|
||||
* <tt>hashCode</tt> but instead inherit the identity based versions
|
||||
* from class <tt>Object</tt>, because element-based equality is not
|
||||
* always well-defined for queues with the same elements but different
|
||||
* ordering properties.
|
||||
*
|
||||
*
|
||||
* <p>This interface is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @see java.util.Collection
|
||||
* @see LinkedList
|
||||
* @see PriorityQueue
|
||||
* @see java.util.concurrent.LinkedBlockingQueue
|
||||
* @see java.util.concurrent.BlockingQueue
|
||||
* @see java.util.concurrent.ArrayBlockingQueue
|
||||
* @see java.util.concurrent.LinkedBlockingQueue
|
||||
* @see java.util.concurrent.PriorityBlockingQueue
|
||||
* @since 1.5
|
||||
* @author Doug Lea
|
||||
* @param <E> the type of elements held in this collection
|
||||
*/
|
||||
public interface Queue<E> extends Collection<E> {
|
||||
/**
|
||||
* Inserts the specified element into this queue if it is possible to do so
|
||||
|
@ -1,50 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* The <code>Stack</code> class represents a last-in-first-out
|
||||
* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
|
||||
* operations that allow a vector to be treated as a stack. The usual
|
||||
* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
|
||||
* method to <tt>peek</tt> at the top item on the stack, a method to test
|
||||
* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
|
||||
* the stack for an item and discover how far it is from the top.
|
||||
* <p>
|
||||
* When a stack is first created, it contains no items.
|
||||
*
|
||||
* <p>A more complete and consistent set of LIFO stack operations is
|
||||
* provided by the {@link Deque} interface and its implementations, which
|
||||
* should be used in preference to this class. For example:
|
||||
* <pre> {@code
|
||||
* Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
|
||||
*
|
||||
* @author Jonathan Payne
|
||||
* @since JDK1.0
|
||||
*/
|
||||
public
|
||||
class Stack<E> extends Vector<E> {
|
||||
/**
|
||||
|
@ -1,108 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* A Red-Black tree based {@link NavigableMap} implementation.
|
||||
* The map is sorted according to the {@linkplain Comparable natural
|
||||
* ordering} of its keys, or by a {@link Comparator} provided at map
|
||||
* creation time, depending on which constructor is used.
|
||||
*
|
||||
* <p>This implementation provides guaranteed log(n) time cost for the
|
||||
* {@code containsKey}, {@code get}, {@code put} and {@code remove}
|
||||
* operations. Algorithms are adaptations of those in Cormen, Leiserson, and
|
||||
* Rivest's <em>Introduction to Algorithms</em>.
|
||||
*
|
||||
* <p>Note that the ordering maintained by a tree map, like any sorted map, and
|
||||
* whether or not an explicit comparator is provided, must be <em>consistent
|
||||
* with {@code equals}</em> if this sorted map is to correctly implement the
|
||||
* {@code Map} interface. (See {@code Comparable} or {@code Comparator} for a
|
||||
* precise definition of <em>consistent with equals</em>.) This is so because
|
||||
* the {@code Map} interface is defined in terms of the {@code equals}
|
||||
* operation, but a sorted map performs all key comparisons using its {@code
|
||||
* compareTo} (or {@code compare}) method, so two keys that are deemed equal by
|
||||
* this method are, from the standpoint of the sorted map, equal. The behavior
|
||||
* of a sorted map <em>is</em> well-defined even if its ordering is
|
||||
* inconsistent with {@code equals}; it just fails to obey the general contract
|
||||
* of the {@code Map} interface.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a map concurrently, and at least one of the
|
||||
* threads modifies the map structurally, it <em>must</em> be synchronized
|
||||
* externally. (A structural modification is any operation that adds or
|
||||
* deletes one or more mappings; merely changing the value associated
|
||||
* with an existing key is not a structural modification.) This is
|
||||
* typically accomplished by synchronizing on some object that naturally
|
||||
* encapsulates the map.
|
||||
* If no such object exists, the map should be "wrapped" using the
|
||||
* {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the map: <pre>
|
||||
* SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by the {@code iterator} method of the collections
|
||||
* returned by all of this class's "collection view methods" are
|
||||
* <em>fail-fast</em>: if the map is structurally modified at any time after
|
||||
* the iterator is created, in any way except through the iterator's own
|
||||
* {@code remove} method, the iterator will throw a {@link
|
||||
* ConcurrentModificationException}. Thus, in the face of concurrent
|
||||
* modification, the iterator fails quickly and cleanly, rather than risking
|
||||
* arbitrary, non-deterministic behavior at an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <em>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</em>
|
||||
*
|
||||
* <p>All {@code Map.Entry} pairs returned by methods in this class
|
||||
* and its views represent snapshots of mappings at the time they were
|
||||
* produced. They do <strong>not</strong> support the {@code Entry.setValue}
|
||||
* method. (Note however that it is possible to change mappings in the
|
||||
* associated map using {@code put}.)
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <K> the type of keys maintained by this map
|
||||
* @param <V> the type of mapped values
|
||||
*
|
||||
* @author Josh Bloch and Doug Lea
|
||||
* @see Map
|
||||
* @see HashMap
|
||||
* @see Hashtable
|
||||
* @see Comparable
|
||||
* @see Comparator
|
||||
* @see Collection
|
||||
* @since 1.2
|
||||
*/
|
||||
|
||||
public class TreeMap<K,V>
|
||||
extends AbstractMap<K,V>
|
||||
implements NavigableMap<K,V>, Cloneable, java.io.Serializable
|
||||
|
@ -1,94 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* A {@link NavigableSet} implementation based on a {@link TreeMap}.
|
||||
* The elements are ordered using their {@linkplain Comparable natural
|
||||
* ordering}, or by a {@link Comparator} provided at set creation
|
||||
* time, depending on which constructor is used.
|
||||
*
|
||||
* <p>This implementation provides guaranteed log(n) time cost for the basic
|
||||
* operations ({@code add}, {@code remove} and {@code contains}).
|
||||
*
|
||||
* <p>Note that the ordering maintained by a set (whether or not an explicit
|
||||
* comparator is provided) must be <i>consistent with equals</i> if it is to
|
||||
* correctly implement the {@code Set} interface. (See {@code Comparable}
|
||||
* or {@code Comparator} for a precise definition of <i>consistent with
|
||||
* equals</i>.) This is so because the {@code Set} interface is defined in
|
||||
* terms of the {@code equals} operation, but a {@code TreeSet} instance
|
||||
* performs all element comparisons using its {@code compareTo} (or
|
||||
* {@code compare}) method, so two elements that are deemed equal by this method
|
||||
* are, from the standpoint of the set, equal. The behavior of a set
|
||||
* <i>is</i> well-defined even if its ordering is inconsistent with equals; it
|
||||
* just fails to obey the general contract of the {@code Set} interface.
|
||||
*
|
||||
* <p><strong>Note that this implementation is not synchronized.</strong>
|
||||
* If multiple threads access a tree set concurrently, and at least one
|
||||
* of the threads modifies the set, it <i>must</i> be synchronized
|
||||
* externally. This is typically accomplished by synchronizing on some
|
||||
* object that naturally encapsulates the set.
|
||||
* If no such object exists, the set should be "wrapped" using the
|
||||
* {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}
|
||||
* method. This is best done at creation time, to prevent accidental
|
||||
* unsynchronized access to the set: <pre>
|
||||
* SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));</pre>
|
||||
*
|
||||
* <p>The iterators returned by this class's {@code iterator} method are
|
||||
* <i>fail-fast</i>: if the set is modified at any time after the iterator is
|
||||
* created, in any way except through the iterator's own {@code remove}
|
||||
* method, the iterator will throw a {@link ConcurrentModificationException}.
|
||||
* Thus, in the face of concurrent modification, the iterator fails quickly
|
||||
* and cleanly, rather than risking arbitrary, non-deterministic behavior at
|
||||
* an undetermined time in the future.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>This class is a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>.
|
||||
*
|
||||
* @param <E> the type of elements maintained by this set
|
||||
*
|
||||
* @author Josh Bloch
|
||||
* @see Collection
|
||||
* @see Set
|
||||
* @see HashSet
|
||||
* @see Comparable
|
||||
* @see Comparator
|
||||
* @see TreeMap
|
||||
* @since 1.2
|
||||
*/
|
||||
|
||||
public class TreeSet<E> extends AbstractSet<E>
|
||||
implements NavigableSet<E>, Cloneable, java.io.Serializable
|
||||
{
|
||||
|
@ -1,81 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package java.util;
|
||||
|
||||
/**
|
||||
* The {@code Vector} class implements a growable array of
|
||||
* objects. Like an array, it contains components that can be
|
||||
* accessed using an integer index. However, the size of a
|
||||
* {@code Vector} can grow or shrink as needed to accommodate
|
||||
* adding and removing items after the {@code Vector} has been created.
|
||||
*
|
||||
* <p>Each vector tries to optimize storage management by maintaining a
|
||||
* {@code capacity} and a {@code capacityIncrement}. The
|
||||
* {@code capacity} is always at least as large as the vector
|
||||
* size; it is usually larger because as components are added to the
|
||||
* vector, the vector's storage increases in chunks the size of
|
||||
* {@code capacityIncrement}. An application can increase the
|
||||
* capacity of a vector before inserting a large number of
|
||||
* components; this reduces the amount of incremental reallocation.
|
||||
*
|
||||
* <p><a name="fail-fast"/>
|
||||
* The iterators returned by this class's {@link #iterator() iterator} and
|
||||
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
|
||||
* if the vector is structurally modified at any time after the iterator is
|
||||
* created, in any way except through the iterator's own
|
||||
* {@link ListIterator#remove() remove} or
|
||||
* {@link ListIterator#add(Object) add} methods, the iterator will throw a
|
||||
* {@link ConcurrentModificationException}. Thus, in the face of
|
||||
* concurrent modification, the iterator fails quickly and cleanly, rather
|
||||
* than risking arbitrary, non-deterministic behavior at an undetermined
|
||||
* time in the future. The {@link Enumeration Enumerations} returned by
|
||||
* the {@link #elements() elements} method are <em>not</em> fail-fast.
|
||||
*
|
||||
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
||||
* as it is, generally speaking, impossible to make any hard guarantees in the
|
||||
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
||||
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
||||
* Therefore, it would be wrong to write a program that depended on this
|
||||
* exception for its correctness: <i>the fail-fast behavior of iterators
|
||||
* should be used only to detect bugs.</i>
|
||||
*
|
||||
* <p>As of the Java 2 platform v1.2, this class was retrofitted to
|
||||
* implement the {@link List} interface, making it a member of the
|
||||
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
||||
* Java Collections Framework</a>. Unlike the new collection
|
||||
* implementations, {@code Vector} is synchronized. If a thread-safe
|
||||
* implementation is not needed, it is recommended to use {@link
|
||||
* ArrayList} in place of {@code Vector}.
|
||||
*
|
||||
* @author Lee Boynton
|
||||
* @author Jonathan Payne
|
||||
* @see Collection
|
||||
* @see LinkedList
|
||||
* @since JDK1.0
|
||||
*/
|
||||
public class Vector<E>
|
||||
extends AbstractList<E>
|
||||
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
|
||||
|
Reference in New Issue
Block a user