CS-Notes/notes/Java 容器.md

362 lines
9.7 KiB
Markdown
Raw Normal View History

2018-02-21 23:28:33 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:22 +08:00
* [????](#????)
2018-02-21 23:28:33 +08:00
* [1. List](#1-list)
* [2. Set](#2-set)
* [3. Queue](#3-queue)
* [4. Map](#4-map)
2018-02-22 14:47:22 +08:00
* [5. Java 1.0/1.1 ????](#5-java-1011-????)
* [?????<3F><>??????](#?????<3F><>??????)
* [1. ????????](#1-????????)
* [2. ????????](#2-????????)
* [???](#???)
* [??????](#??????)
2018-02-21 23:28:33 +08:00
* [1. ArraList](#1-arralist)
2018-02-22 14:47:22 +08:00
* [2. Vector ?? Stack](#2-vector-??-stack)
2018-02-21 23:28:33 +08:00
* [3. LinkedList](#3-linkedlist)
* [4. TreeMap](#4-treemap)
* [5. HashMap](#5-hashmap)
* [6. LinkedHashMap](#6-linkedhashmap)
* [7. ConcurrentHashMap](#7-concurrenthashmap)
2018-02-22 14:47:22 +08:00
* [?<3F><>?????](#?<3F><>?????)
2018-02-21 23:28:33 +08:00
<!-- GFM-TOC -->
2018-02-22 14:47:22 +08:00
# ????
2018-02-21 23:28:33 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ebf03f56-f957-4435-9f8f-0f605661484d.jpg)
2018-02-22 14:47:22 +08:00
??????????? Collection ?? Map ?????Collection ??????? List??Set ??? Queue??
2018-02-21 23:28:33 +08:00
## 1. List
2018-02-22 14:47:22 +08:00
- ArrayList???????????????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- LinkedList????????????????????????????????????????????????<3F><>????????????????????LinkedList ????????????????<3F><>??????<3F><>?
2018-02-21 23:28:33 +08:00
## 2. Set
2018-02-22 14:47:22 +08:00
- HashSet?????? Hash ???????????????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- TreeSet????????????????????????????<3F><>????? HashSet??
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- LinkedListHashSet?????? HashSet ?????<3F><>?????????????????????????????????????????
2018-02-21 23:28:33 +08:00
## 3. Queue
2018-02-22 14:47:22 +08:00
???????????LinkedList ?? PriorityQueue?????? LinkedList ????????<3F><>?PriorityQueue ???????????
2018-02-21 23:28:33 +08:00
## 4. Map
2018-02-22 14:47:22 +08:00
- HashMap?????? Hash ???
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- LinkedHashMap??????????????????????????????????????????????LRU?????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- TreeMap?????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- ConcurrentHashMap??????? Map?????<3F>p?????? HashTable ?????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
## 5. Java 1.0/1.1 ????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- Vector???? ArrayList ??????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- HashTable???? HashMap ??????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
# ?????<3F><>??????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
## 1. ????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????? Iterator ??????????????????????????????????<3F><>?????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
[Java ?<3F><>???????? ](https://github.com/CyC2018/InterviewNotes/blob/master/notes/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F.md#92-java-%E5%86%85%E7%BD%AE%E7%9A%84%E8%BF%AD%E4%BB%A3%E5%99%A8)
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
## 2. ????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
java.util.Arrays#asList() ????????????????? List ?????
2018-02-21 23:28:33 +08:00
```java
List list = Arrays.asList(1, 2, 3);
int[] arr = {1, 2, 3};
list = Arrays.asList(arr);
```
2018-02-22 14:47:22 +08:00
# ???
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
??? hasCode() ??????????????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
?? equals() ???????<3F><>??????????????????????????????????????????????????????????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
1. ?????
2. ?????
3. ??????
4. ????????<3F><>??? x.equals(y)?????????
5. ???<3F>ʦ<EFBFBD>??? null ????? x ???? x.equals(nul) ?????? false
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
# ??????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
????????? [ ?? - ???? ](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) ????????????????????<3F><>???????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
????????[OpenJDK 1.7](http://download.java.net/openjdk/jdk7)
2018-02-21 23:28:33 +08:00
## 1. ArraList
[ArraList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/ArrayList.java)
2018-02-22 14:47:22 +08:00
????? RandomAccess ??????????????????????????????????? ArrayList ??????????????
2018-02-21 23:28:33 +08:00
```java
2018-02-21 23:29:42 +08:00
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
2018-02-21 23:28:33 +08:00
```
2018-02-22 14:47:22 +08:00
??????????????????????????? transient ???<3F><>?????????????<3F><>???????<3F><>????????????????????????????????<3F><>????????<3F><> writeObject() ?? readObject()??
2018-02-21 23:28:33 +08:00
```java
private transient Object[] elementData;
```
2018-02-22 14:47:22 +08:00
?????????<3F><>? 10
2018-02-21 23:28:33 +08:00
```java
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
this.elementData = new Object[initialCapacity];
}
public ArrayList() {
this(10);
}
```
2018-02-22 14:47:22 +08:00
??????????? System.arraycopy() ???????<3F><>?????????????????????????????????????????????<3F><>????????????????<3F><>?????
2018-02-21 23:28:33 +08:00
```java
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
```
2018-02-22 14:47:22 +08:00
?????????? ensureCapacity() ????????????????????????????????????????????????????????? 1.5 ????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
modCount ??????? ArrayList ?????<3F><>?????????????????? add() ?? addAll() ?????????? ensureCapacity()?????????? ensureCapacity() ?<3F><>? modCount ????????
2018-02-21 23:28:33 +08:00
```java
public void ensureCapacity(int minCapacity) {
if (minCapacity > 0)
ensureCapacityInternal(minCapacity);
}
private void ensureCapacityInternal(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
```
2018-02-22 14:47:22 +08:00
????????<3F><>???????????????????????????? modCount ??????????????????? ConcurrentModificationException??
2018-02-21 23:28:33 +08:00
```java
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out array length
s.writeInt(elementData.length);
// Write out all elements in the proper order.
for (int i=0; i<size; i++)
s.writeObject(elementData[i]);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
```
2018-02-22 14:47:22 +08:00
**?? Vector ??????**
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
1. Vector ?? ArrayList ???????????????<3F><>??????????? Vector ????????????????? ArrayList ????????????????? ArrayList ?????? Vector???????????????????????????????
2. Vector ??????????????<3F><>?? 2 ??????? ArrayList ?? 1.5 ????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
????????????? ArrayList????????? Collections.synchronizedList(new ArrayList<>()); ?????????????? ArrayList?????????? concurrent ????????? CopyOnWriteArrayList ??
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
**?? LinkedList ??????**
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
1. ArrayList ??????????????LinkedList ??????????????????
2. ArrayList ???????????LinkedList ??????
3. LinkedList ??????<3F><>??????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
## 2. Vector ?? Stack
2018-02-21 23:28:33 +08:00
[Vector.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/Vector.java)
## 3. LinkedList
[LinkedList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/LinkedList.java)
## 4. TreeMap
[TreeMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/TreeMap.java)
## 5. HashMap
[HashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
2018-02-22 14:47:22 +08:00
???????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
??????? capacity ? 16????????????????????? 2 ??<3F><>??????????? Entry[] table ?????????size ?????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
threshold ?<3F><>????? size ????????size ????<3F><>?? threshold??????????????????????????????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
threshold = capacity * load_factor?????? load_factor ? table ????????????????load_factor ????????????????????????????<3F><>????????????
2018-02-21 23:28:33 +08:00
```java
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final int MAXIMUM_CAPACITY = 1 << 30;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
transient Entry[] table;
transient int size;
int threshold;
final float loadFactor;
transient int modCount;
```
2018-02-22 14:47:22 +08:00
?????????????????<3F><>?????????????????????? capacity ????????????
2018-02-21 23:28:33 +08:00
```java
void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
table[bucketIndex] = new Entry<>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
```
2018-02-22 14:47:22 +08:00
Entry ??????????????????????<3F><>? next ????????<3F><>??????<3F><>?
2018-02-21 23:28:33 +08:00
```java
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
}
```
2018-02-22 14:47:22 +08:00
get() ???????????????????key ? null ?? ??? null?????<3F><>?????? HashMap ??????? null ???????
2018-02-21 23:28:33 +08:00
```java
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}
```
2018-02-22 14:47:22 +08:00
put() ???????????? key ???? null ????????????????????????????? key ? null ??????????????? key ? null ???????????????????? 0 <20><>?????????? null ??????? hash ??????????????<3F><>?????????????
2018-02-21 23:28:33 +08:00
```java
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
```
```java
private V putForNullKey(V value) {
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
if (e.key == null) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(0, null, value, 0);
return null;
}
```
## 6. LinkedHashMap
[LinkedHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
## 7. ConcurrentHashMap
[ConcurrentHashMap.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/HashMap.java)
2018-02-22 14:47:22 +08:00
[ ??? ConcurrentHashMap ?????????????? ](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/)
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
# ?<3F><>?????
2018-02-21 23:28:33 +08:00
2018-02-22 14:47:22 +08:00
- Java ??????