Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What load factor should be used when you know maximum possible no of elements in HashSet

What load factor should I use when I really know the maximum possible no of elements in a HashSet ? I had heard that the default load factor of 0.75 is recommended as it offers good performance trade-offs between speed & space. Is this correct ? However a larger size HashSet would also takes more time in creation and more space.

I am using HashSet just inorder to remove duplicate integers from a list of integers.

like image 353
Rajat Gupta Avatar asked Mar 13 '11 19:03

Rajat Gupta


People also ask

What is the load factor of HashSet?

Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor, which is 0.75 .

What is load factor in Java HashSet?

The load factor is a measure of how full the HashSet is allowed to get before its capacity is automatically increased.

What is the load factor for a hash table?

Overview. Load factor is defined as (m/n) where n is the total size of the hash table and m is the preferred number of entries which can be inserted before a increment in size of the underlying data structure is required.

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


2 Answers

I spent some time playing around with load factors once, and it is shocking how little difference that setting really makes in practice. Even setting it to something high like 2.0 doesn't slow things down much, nor does it save that much memory. Just pretend it doesn't exist. Josh has often regretted ever exposing it as an option at all.

like image 119
Kevin Bourrillion Avatar answered Oct 01 '22 20:10

Kevin Bourrillion


For your stated problem, instead of using a HashSet, you might also consider a BitSet

Depending on the range and sparsity of your integers, you may get better performance and space characteristics.

like image 45
Bill Michell Avatar answered Oct 01 '22 19:10

Bill Michell