Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashSet load factor

Tags:

java

hashset

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?

like image 655
xdevel2000 Avatar asked Aug 25 '10 09:08

xdevel2000


People also ask

What is initial capacity and load factor in HashSet?

Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).

What is the load factor of set in Java?

The load factor is the measure that decides when to increase the capacity of the Map. The default load factor is 75% of the capacity. The threshold of a HashMap is approximately the product of current capacity and load factor. Rehashing is the process of re-calculating the hash code of already stored entries.

What is the default value of load factor in HashMap?

Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75). Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).


3 Answers

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

like image 71
Sheo Avatar answered Oct 24 '22 00:10

Sheo


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.

like image 8
Vivek Goel Avatar answered Oct 24 '22 00:10

Vivek Goel


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

like image 3
Riduidel Avatar answered Oct 24 '22 02:10

Riduidel