hashmap如何保证每个hash值在hashtable中被赋予唯一的索引
how does hashmap ensure that each hash value is assigned a unique index in the hash table
我想了解 HashMap 的 hash() 和 indexOf() 方法如何将哈希 table 中的唯一索引分配给哈希图。也就是说,为什么不能将多个哈希值映射到同一个索引。
在 HashMap
中,基础桶数组大小在初始化时设置,并且可以相应地调整大小 - 项目的桶索引(在 indexFor
中)由 key.hashCode() % (table.length - 1)
生成。
how does hashmap ensure that each hash value is assigned a unique index in the hash table
不必是唯一的(见下文)
why it is not possible for multiple hash values to be mapped to the same index
这是可能的 - 可以将多个 Entry
(key/value 对)映射到单个 bucket
。适当的哈希 table 实现通过让每个 bucket
能够容纳多个 Entry
来克服这个问题。 HashMap
特别使用链表 - 如果一个项目映射到一个已经被占用的 bucket
,则该项目被添加到存储桶链表的开头。
我想了解 HashMap 的 hash() 和 indexOf() 方法如何将哈希 table 中的唯一索引分配给哈希图。也就是说,为什么不能将多个哈希值映射到同一个索引。
在 HashMap
中,基础桶数组大小在初始化时设置,并且可以相应地调整大小 - 项目的桶索引(在 indexFor
中)由 key.hashCode() % (table.length - 1)
生成。
how does hashmap ensure that each hash value is assigned a unique index in the hash table
不必是唯一的(见下文)
why it is not possible for multiple hash values to be mapped to the same index
这是可能的 - 可以将多个 Entry
(key/value 对)映射到单个 bucket
。适当的哈希 table 实现通过让每个 bucket
能够容纳多个 Entry
来克服这个问题。 HashMap
特别使用链表 - 如果一个项目映射到一个已经被占用的 bucket
,则该项目被添加到存储桶链表的开头。