xdevel2000
xdevel2000

Reputation: 21444

HashSet load factor

If I use a HashSet with a initial capacity of 10 and a load factor of 0.5 then every 5 elements added the HashSet will be increased or first the HashSet is increased of 10 elements and after at 15 at 20 atc. the capacity will be increased?

Upvotes: 21

Views: 18377

Answers (3)

Vivek Goel
Vivek Goel

Reputation: 782

Default initial capacity of the HashMap takes is 16 and load factor is 0.75f (i.e 75% of current map size). The load factor represents at what level the HashMap capacity should be doubled.

For example product of capacity and load factor as 16 * 0.75 = 12. This represents that after storing the 12th key – value pair into the HashMap , its capacity becomes 32.

Upvotes: 8

Sheo
Sheo

Reputation: 1044

The load factor is a measure of how full the HashSet is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

source

Upvotes: 31

Riduidel
Riduidel

Reputation: 22308

It's the second case. The loadFactor of both HashSet and hashMap is a relative factor.

Upvotes: 3

Related Questions