如果不同的对象(比如3)具有相同的哈希码,那么应该是map的大小,因此,存在于同一个桶中?

时间:2022-10-13 16:48:43

What should be size of map if different objects(say 3) have same hash code, and as a result, present in same bucket?

如果不同的对象(比如3)具有相同的哈希码,那么应该是map的大小,因此,存在于同一个桶中?

1 个解决方案

#1


0  

The resulting size of the hash table depends on what collision resolution scheme we are using.

哈希表的结果大小取决于我们使用的冲突解决方案。

In the simplest case, we are using something like separate chaining (with linked lists).

在最简单的情况下,我们使用的是单独的链接(链接列表)。

In this case, we will have an array of N buckets and each bucket contains a reference to a linked list.

在这种情况下,我们将有一个N桶数组,每个桶包含对链表的引用。

If we proceed to insert 3 items into the hash table, all of which share the same hash code, then the single target linked list would grow to length 3.

如果我们继续在哈希表中插入3个项目,所有这些项目共享相同的哈希码,那么单个目标链接列表将增长到3。

Thus, at a high level, we need at least N "units" of space to store bucket references plus 3 "units" of space to store the elements of the (occupied) linked list.

因此,在高级别,我们需要至少N个“单位”的空间来存储桶引用加上3个“单位”的空间来存储(占用的)链表的元素。

The exact size of these "units", depends on implementation details, such as word size (32-bit vs. 64-bit) and the exact definition of the linked list (singly- vs. doubly-linked list).

这些“单元”的确切大小取决于实现细节,例如字长(32位与64位)以及链表的确切定义(单链与双链表)。

Assuming that we use singly-linked lists (for each bucket) on a 32-bit machine, the total size would be (approximately) 32 * N + (32 + x) * 3, where x refers to the size of the data type we are storing (e.g. ints, doubles, string, etc.)

假设我们在32位机器上使用单链表(对于每个桶),总大小将是(大约)32 * N +(32 + x)* 3,其中x表示数据类型的大小我们正在存储(例如,整数,双打,字符串等)

If you would like to learn more, I would suggest googling "hash table collision" for more info.

如果您想了解更多信息,我建议谷歌搜索“哈希表冲突”了解更多信息。

#1


0  

The resulting size of the hash table depends on what collision resolution scheme we are using.

哈希表的结果大小取决于我们使用的冲突解决方案。

In the simplest case, we are using something like separate chaining (with linked lists).

在最简单的情况下,我们使用的是单独的链接(链接列表)。

In this case, we will have an array of N buckets and each bucket contains a reference to a linked list.

在这种情况下,我们将有一个N桶数组,每个桶包含对链表的引用。

If we proceed to insert 3 items into the hash table, all of which share the same hash code, then the single target linked list would grow to length 3.

如果我们继续在哈希表中插入3个项目,所有这些项目共享相同的哈希码,那么单个目标链接列表将增长到3。

Thus, at a high level, we need at least N "units" of space to store bucket references plus 3 "units" of space to store the elements of the (occupied) linked list.

因此,在高级别,我们需要至少N个“单位”的空间来存储桶引用加上3个“单位”的空间来存储(占用的)链表的元素。

The exact size of these "units", depends on implementation details, such as word size (32-bit vs. 64-bit) and the exact definition of the linked list (singly- vs. doubly-linked list).

这些“单元”的确切大小取决于实现细节,例如字长(32位与64位)以及链表的确切定义(单链与双链表)。

Assuming that we use singly-linked lists (for each bucket) on a 32-bit machine, the total size would be (approximately) 32 * N + (32 + x) * 3, where x refers to the size of the data type we are storing (e.g. ints, doubles, string, etc.)

假设我们在32位机器上使用单链表(对于每个桶),总大小将是(大约)32 * N +(32 + x)* 3,其中x表示数据类型的大小我们正在存储(例如,整数,双打,字符串等)

If you would like to learn more, I would suggest googling "hash table collision" for more info.

如果您想了解更多信息,我建议谷歌搜索“哈希表冲突”了解更多信息。