CS-Notes/notes/Java 容器.md
2018-02-22 14:47:22 +08:00

362 lines
9.7 KiB
Markdown
Raw Blame History

<!-- GFM-TOC -->
* [????](#????)
* [1. List](#1-list)
* [2. Set](#2-set)
* [3. Queue](#3-queue)
* [4. Map](#4-map)
* [5. Java 1.0/1.1 ????](#5-java-1011-????)
* [?????§Ö??????](#?????§Ö??????)
* [1. ????????](#1-????????)
* [2. ????????](#2-????????)
* [???](#???)
* [??????](#??????)
* [1. ArraList](#1-arralist)
* [2. Vector ?? Stack](#2-vector-??-stack)
* [3. LinkedList](#3-linkedlist)
* [4. TreeMap](#4-treemap)
* [5. HashMap](#5-hashmap)
* [6. LinkedHashMap](#6-linkedhashmap)
* [7. ConcurrentHashMap](#7-concurrenthashmap)
* [?¦Ï?????](#?¦Ï?????)
<!-- GFM-TOC -->
# ????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ebf03f56-f957-4435-9f8f-0f605661484d.jpg)
??????????? Collection ?? Map ?????Collection ??????? List??Set ??? Queue??
## 1. List
- ArrayList???????????????????????????
- LinkedList????????????????????????????????????????????????§Þ????????????????????LinkedList ????????????????§Ü??????§³?
## 2. Set
- HashSet?????? Hash ???????????????????????????
- TreeSet????????????????????????????§¹????? HashSet??
- LinkedListHashSet?????? HashSet ?????§¹?????????????????????????????????????????
## 3. Queue
???????????LinkedList ?? PriorityQueue?????? LinkedList ????????§µ?PriorityQueue ???????????
## 4. Map
- HashMap?????? Hash ???
- LinkedHashMap??????????????????????????????????????????????LRU?????
- TreeMap?????????????
- ConcurrentHashMap??????? Map?????<3F>p?????? HashTable ?????????
## 5. Java 1.0/1.1 ????
??????????????????????????????????????????????
- Vector???? ArrayList ??????????????????
- HashTable???? HashMap ??????????????????
# ?????§Ö??????
## 1. ????????
????????????????????????????? Iterator ??????????????????????????????????§Ö?????
[Java ?§Ö???????? ](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. ????????
java.util.Arrays#asList() ????????????????? List ?????
```java
List list = Arrays.asList(1, 2, 3);
int[] arr = {1, 2, 3};
list = Arrays.asList(arr);
```
# ???
??? hasCode() ??????????????????????????
?? equals() ???????§Ø??????????????????????????????????????????????????????????????????????
??????????????????????
1. ?????
2. ?????
3. ??????
4. ????????¦Å??? x.equals(y)?????????
5. ???¦Ê¦Â??? null ????? x ???? x.equals(nul) ?????? false
# ??????
????????? [ ?? - ???? ](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) ????????????????????§Ü???????
????????[OpenJDK 1.7](http://download.java.net/openjdk/jdk7)
## 1. ArraList
[ArraList.java](https://github.com/CyC2018/InterviewNotes/blob/master/notes/src/ArrayList.java)
????? RandomAccess ??????????????????????????????????? ArrayList ??????????????
```java
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
```
??????????????????????????? transient ???¦²?????????????öö???????¦Ë????????????????????????????????§Ý????????§Õ writeObject() ?? readObject()??
```java
private transient Object[] elementData;
```
?????????§³? 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);
}
```
??????????? System.arraycopy() ???????§Ú?????????????????????????????????????????????§³????????????????§Õ?????
```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;
}
```
?????????? ensureCapacity() ????????????????????????????????????????????????????????? 1.5 ????
modCount ??????? ArrayList ?????<3F>£?????????????????? add() ?? addAll() ?????????? ensureCapacity()?????????? ensureCapacity() ?§Ø? modCount ????????
```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;
}
```
????????§Ý???????????????????????????? modCount ??????????????????? ConcurrentModificationException??
```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();
}
}
```
**?? Vector ??????**
1. Vector ?? ArrayList ???????????????¦·??????????? Vector ????????????????? ArrayList ????????????????? ArrayList ?????? Vector???????????????????????????????
2. Vector ??????????????§³?? 2 ??????? ArrayList ?? 1.5 ????
????????????? ArrayList????????? Collections.synchronizedList(new ArrayList<>()); ?????????????? ArrayList?????????? concurrent ????????? CopyOnWriteArrayList ??
**?? LinkedList ??????**
1. ArrayList ??????????????LinkedList ??????????????????
2. ArrayList ???????????LinkedList ??????
3. LinkedList ??????¦Ë??????????????
## 2. Vector ?? 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)
???????????????????
??????? capacity ? 16????????????????????? 2 ??¦Ç??????????? Entry[] table ?????????size ?????????????????
threshold ?ÕÇ????? size ????????size ????§³?? threshold??????????????????????????????
threshold = capacity * load_factor?????? load_factor ? table ????????????????load_factor ????????????????????????????§¹????????????
```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;
```
?????????????????§á?????????????????????? capacity ????????????
```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 ??????????????????????§Ö? next ????????§Ý??????¨¢?
```java
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;
}
```
get() ???????????????????key ? null ?? ??? null?????§á?????? HashMap ??????? null ???????
```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() ???????????? key ???? null ????????????????????????????? key ? null ??????????????? key ? null ???????????????????? 0 ¦Ë?????????? null ??????? hash ??????????????¡Â?????????????
```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)
[ ??? ConcurrentHashMap ?????????????? ](https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/)
# ?¦Ï?????
- Java ??????