为散列选择合适的表大小 [英] Chosing a suitable table size for a Hash

查看:135
本文介绍了为散列选择合适的表大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个1000的密钥集,我的哈希表的适当大小是什么,以及如何确定它?

If I have a key set of 1000, what is a suitable size for my Hash table, and how is that determined?

推荐答案

它取决于负载因子(表格将增加其大小并重新分配其元素的完整百分比点)。如果您知道您有1000个条目,并且该数字永远不会更改,您可以将负载因子设置为1.0,并将初始大小设置为1000以获得最大效率。如果您不确定实际尺寸,则可以将载荷系数保持为默认值0.75,并将初始尺寸设置为1334(预期尺寸/ LF),以确保 < 性能良好,但费用的额外内存。

It depends on the load factor (the "percent full" point where the table will increase its size and re-distribute its elements). If you know you have exactly 1000 entries, and that number will never change, you can just set the load factor to 1.0 and the initial size to 1000 for maximum efficiency. If you weren't sure of the exact size, you could leave the load factor at its default of 0.75 and set your initial size to 1334 (expected size/LF) for really good performance, at a cost of extra memory.

您可以使用以下构造函数来设置加载因子:

You can use the following constructor to set the load factor:

Hashtable(int initialCapacity, float loadFactor) 

这篇关于为散列选择合适的表大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆