1.HashMap的get流程
分析代码:
public V get(Object key) {HashMap.Node<K,V> e;/*** 1.计算key的hash值* e:获取的结果*/return (e = getNode(hash(key), key)) == null ? null : e.value;
}final HashMap.Node<K,V> getNode(int hash, Object key) {HashMap.Node<K,V>[] tab; HashMap.Node<K,V> first, e; int n; K k;/*** 1.tab = table* 2.计算下标,取出对象赋值给 first*/if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) {/*** key值相同&hash相同 直接返回 first*/if (first.hash == hash && ((k = first.key) == key || (key != null && key.equals(k))))return first;/*** first.next : e 说明是链表或者红黑树*/if ((e = first.next) != null) {// 进入红黑树逻辑