Search code examples
javahashhashtable

Chosing a suitable table size for a Hash


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


Solution

  • 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)