* [????](#????) * [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) * [?羊?????](#?羊?????) # ???? ![](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?????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 extends AbstractList implements List, 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 ??????????????????????? 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()); ?????????????? 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 e = table[bucketIndex]; table[bucketIndex] = new Entry<>(hash, key, value, e); if (size++ >= threshold) resize(2 * table.length); } ``` Entry ??????????????????????快? next ????????抖??????芍? ```java static class Entry implements Map.Entry { final K key; V value; Entry 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 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 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 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 ??????