Java 哈希表(如 `HashMap`)扩容机制的核心说明
·
| 英文句子 | 中文翻译 | 生词/专业词标注与翻译 | 句子结构拆解 |
|---|---|---|---|
| Initializes or doubles table size. | 初始化或加倍表的大小。 | - Initializes:初始化 - doubles:使加倍 - table:表(此处指哈希表) |
简单主谓宾结构,“Initializes or doubles”是谓语,“table size”是宾语。 |
| If null, allocates in accord with initial capacity target held in field threshold. | 如果表为 null,则按照字段 threshold 中保存的初始容量目标进行分配。 | - allocates:分配 - in accord with:按照 - initial capacity:初始容量 - target:目标 - held:保存(过去分词作后置定语) - field:字段(计算机术语,类或对象的属性) - threshold:阈值(哈希表的阈值字段) |
条件状语从句(If null)+ 主句(allocates…);“held in field threshold”是过去分词短语作后置定语,修饰“initial capacity target”。 |
| Otherwise, because we are using power-of-two expansion, the elements from each bin must either stay at same index, or move with a power of two offset in the new table. | 否则,因为我们使用 2 的幂次扩容,每个桶中的元素必须要么留在相同的索引处,要么在新表中以 2 的幂次偏移量移动。 | - power-of-two:2 的幂次 - expansion:扩容 - elements:元素 - bin:桶(哈希表中存储元素的桶) - index:索引 - offset:偏移量 |
原因状语从句(because we are using…)+ 主句(the elements… must either… or…);“from each bin”是介词短语作后置定语修饰“elements”,“with a power of two offset”是介词短语作方式状语。 |
背景与用途分析
这段内容属于 Java 哈希表(如 HashMap)扩容机制的核心说明。哈希表在元素数量超过阈值(threshold)时会触发扩容(resize),这里解释了扩容时的两种情况:若表未初始化则按初始容量分配,若已初始化则以 2 的幂次扩容,且每个桶的元素要么留在原索引,要么以 2 的幂次偏移到新索引,这一机制保障了哈希表扩容后元素分布的效率与合理性。
更多推荐
所有评论(0)