Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HashTable LoadFactor

The Java Hashtable has a constructor where you can specify a loadFactor. However, if the initialCapacity (n) is known, what's the point of specifying the loadFactor?

Assuming that the size of its array of buckets (s) is constant, does the constructor Hashtable(int initialCapacity, float loadFactor) just create a Hash Table that has more capacity than initialCapacity to ensure the correct loadFactor?

like image 692
rdasxy Avatar asked Mar 18 '12 21:03

rdasxy


1 Answers

Assuming that the size of its array of buckets (s) is constant, […]

This assumption is not correct. The array of buckets is resized when necessary to ensure that the proportion of non-empty buckets is at most loadFactor.

(Note: the Javadoc states that "The initial capacity and load factor parameters are merely hints to the implementation. The exact details as to when and whether the rehash method is invoked are implementation-dependent", so the above shouldn't be taken as a strict guarantee. But it's the general behavior.)

like image 197
ruakh Avatar answered Sep 18 '22 01:09

ruakh