HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | public V put(K key, V value) { // 1. 初始化 if (table == EMPTY_TABLE) { inflateTable(threshold); } // 2. 允许key为null的put if (key == null ) return putForNullKey(value); // 3. 首先通过Hash计算Hash值,然后计算数组索引 int hash = hash(key); int i = indexFor(hash, table.length); // 4. 处理冲突,遍历链表重新设置新值返回老值 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; } } // 5. 无冲突,增加新值 modCount++; addEntry(hash, key, value, i); return null ; } void addEntry( int hash, K key, V value, int bucketIndex) { // 1. 超过指定阀值进行扩容处理 if ((size >= threshold) && ( null != table[bucketIndex])) { resize( 2 * table.length); hash = ( null != key) ? hash(key) : 0 ; bucketIndex = indexFor(hash, table.length); } // 2. 创建数组对应的值 createEntry(hash, key, value, bucketIndex); } void createEntry( int hash, K key, V value, int bucketIndex) { Entry<K,V> e = table[bucketIndex]; table[bucketIndex] = new Entry<>(hash, key, value, e); size++; } |
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库