简体   繁体   English

从哈希表中删除元素时,c#如何缩小?

[英]c# how hashtable shrinks when elements are removed from hashtable?

I am looking to find out the logic , if any , which shrinks hashtable in c# when elements are removed from it. 我正在寻找找出逻辑,如果有的话,当从中删除元素时,它会缩小c#中的哈希表。

Regards Harish 关于harish

c# hashtables don't shrink; C#哈希表不会缩小; they only grow. 他们只会增长。 The logic is important, because the rehashing algorithm is VERY expensive to run; 逻辑很重要,因为重新哈希算法的运行成本非常高; for most situations, the space saved by rehashing into a smaller hashtable would be completely overrun by the cost of the rehashing. 在大多数情况下,通过重新哈希化为较小的哈希表节省的空间将完全被重新哈希化的开销所覆盖。 Particularly on an automatic basis, where any removals from the hashtable may not be the "last" removal (impossible to tell from within the hashtable on an algorithmic basis), the potential value is simply not worth it. 特别是在自动的基础上,从哈希表中删除的内容可能不是“最后”删除(不可能在算法上从哈希表中看出),潜在值根本不值得。

If your hashtable shrinks significantly, and you'd really like to reclaim the space, I recommend simply creating a new one (with the right size) and copying the elements over to it. 如果您的哈希表大大缩小,并且您真的想回收空间,我建议您简单地创建一个新的(具有正确的大小)并将其复制到该元素上。

顺便说一句,由于您使用的是.net2.0或更高版本,因此您可能应该使用Dictionary<K,V>而不是HashTable。

The only indication of size changes for a hashtable in the documentation is when the load factor is exceeded and the size of the hashtable is increased. 文档中哈希表大小更改的唯一指示是何时超过了负载因子并且哈希表的大小增加了。 There is no mention of a hastable ever shrinking. 没有提到一个持续缩小的趋势。

There is some further detail of the load factor on MSDN . 有关MSDN上负载因子的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM