企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] > ### `HashMap`类图 ![](https://i.loli.net/2019/03/17/5c8e4002d11b9.png) <br/> > ### 构造函数 ``` // DEFAULT_LOAD_FACTOR = 0.75 // initialCapacity 16 public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted } public HashMap(int initialCapacity, float loadFactor) { } ``` <br/> > ### 哈希算法 * 在原生`hashCode`的基础上做了一次与高16位相异或的处理,这样做的目的是将哈希值的高位纳入到取余运算中来,防止由于低位相同造成的频繁冲突的情况。 * `hashCode()`,默认情况下,`Object`中的`hashCode()` 返回对象的32位jvm内存地址。也就是说如果对象不重写该方法,则返回相应对象的32为JVM内存地址。 ``` static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } ``` <br/> > ### `put` * onlyIfAbsent参数如果为true,那么对于已经存在的key,将不替换其值 。 * `transient`修饰的变量在类实例被序列化时不会被序列化。 * table即HashMap进行数据存储的核心变量:`transient Node<K,V>[] table;` ``` final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //1.初始化 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; // Initializes (16) or doubles table size //2.bin为null,初始化第一个节点,通过(n - 1) & hash定位在数组中的位置 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)))) //指定key的值已存在,那么记录下原先的值 e = p; else if (p instanceof TreeNode) //红黑树 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); else { //bin不为空,且与链表头不相同(==或equals) //3. for (int binCount = 0; ; ++binCount) { if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); //达到临界值(8)转为红黑树 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; //空实现,为LinkedHashMap预留 afterNodeAccess(e); return oldValue; } } ++modCount; //4. if (++size > threshold) resize(); //空实现,为LinkedHashMap预留 afterNodeInsertion(evict); return null; } ``` <br/> > ### `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; //原table不为null, if (oldCap > 0) { //MAXIMUM_CAPACITY取1 << 30,即table数组的大小,如果已到达此值,那么无需扩容 if (oldCap >= MAXIMUM_CAPACITY) { //threshold,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 { //初始化,默认大小为16 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; 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; } ``` <br/> <br/> *** 参考: [HashMap](https://github.com/seaswalker/JDK/blob/master/note/HashMap/hashmap.md)