java中集合框架是常用的的API。有必要做一个使用总结。接口结构如下
Map<K,V> -->SortedMap --> NavigableMap
Iterable<E> -->Collection<E> -->Queue/List/Set -->Deque/SortedSet
类继承结构如下:
1-java.lang.Object
1.1-AbstractCollection类
1.1.1-AbstractList类
1.1.1.1-AbstractSequentialList
1.1.1.1.1-LinkedList
1.1.1.2-ArrayList
1.1.1.2.1-ObesrableArrayList
1.1.1.3-Vector
1.1.1.3.1-Stack
1.1.2-AbstractQueue类
1.1.2.1-ArrayBlockingQueue
1.1.2.2-ConcurrentLinkedQueue
1.1.2.3-DelayQueue
1.1.2.4-LinkedBlockDeque
1.1.2.5-LinkedBlockingQueue
1.1.2.6-LinkedTransferQueue
1.1.2.7-PriorityBolckingQueue
1.1.2.8-PriorityQueue
1.1.2.9-SynchronousQueue
1.1.3-AbstractSet类
1.1.3.1-ConcurrentSkipListSet
1.1.3.1-CopyOnWriteArraySet
1.1.3.2-EnumSet
1.1.3.3-HashSet
1.1.3.4-TreeSet
1.1.4-ArrayDeque类 双端队列 asynctask源码用到
1.1.5-ConcurrentLinkedDeque类
整体的一个目录机构就如上所示,里面囊括了所有List和Set相关的。
Map类
1-AbstractMap(extends Object)类
1.1-ConcurrentHashMap
1,2-ConcurrentSkipListMap
1.3-EnumMap
1.4-HashMap
1.4.1-LinkedHashMap
1.5-IdentityHashMap
1.6-TreeMap
1.7-WeakHashMap
Dictionary类
1-Dictionary
1.1-HashTable
1.1.1-Properties
1.1.1.1-Provider
1.1.1.1.1-AuthProvider
接下来是有些常见的问题索引。
1 HashMap和HashTable底层实现有什么区别?
2 hashMap和ConcurrentHashMap呢?
3 HashMap和TreeMap有什么区别?底层数据结构是什么?
4 Java运行时环境是如何判断HashSet中相同对象、HashMap中相同键的呢?
5 HashMap和HashSet的区别
hashmap源码解析
putVaule(k,v)方法:
image.png
接着调用:
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
可以看出:
1.如果为空或者size为零,就resize扩容一次;
2.如果插入的key不存在,就新增一个node;
3,如果存在,就替换对应key的值,如果是树节点,就替换树节点的值,
4,如果超过threshold阀值,就调用resize扩容方法。
resize方法:
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
}
Arrays的equals方法:
public static boolean equals(Object[] a, Object[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++) {
Object o1 = a[i];
Object o2 = a2[i];
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return true;
}
binarySearch0查找方法:
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}