1

I heard, that load factor in HashMap reinstates the buckets to other location and it's better to keep it at 0.75, so that when the size touches 0.75*present capacity the array of buckets reallocates to twice the present capacity

For instance, we have capacity 16; the array reallocates when the size become 16*0.75 = 12. At this point, we are creating extra 16 elements even before the array touches 16, because it's memory inefficient.

If it's time efficient, how it becomes or are there any trade offs to use load factor?

Mysterion
  • 9,050
  • 3
  • 30
  • 52
Pavan Kumar
  • 77
  • 1
  • 6
  • Welcome to SO, try and search for relevant questions before posting your own! You may find this question informative http://stackoverflow.com/questions/10901752/what-is-the-significance-of-load-factor-in-hashmap – HavelTheGreat Jan 27 '15 at 20:38
  • 1
    This question doesn't appear to be related to functional programming. – Peter Lawrey Jan 27 '15 at 20:39
  • "meaning it is memory inefficient." -> so you mean the hashtable should wait to get filled up completely before allocating new memory? memory efficiency (in your terms) - this is not the only factor here; read up why load factor of 0.75 is recommended. – peter.petrov Jan 27 '15 at 20:42
  • Also if you happen to have a custom hash code function and it is bad (e.g. int hashCode() { return 0; } ) even 0.75 factor will not make any good. – yurgis Jan 27 '15 at 20:54
  • "Filled up" is a weird concept. You're allocating 16 extra _buckets_, but a bucket does not necessarily correspond to one entry. Buckets are actually relatively cheap compared to the entries put in them. – Louis Wasserman Jan 27 '15 at 21:06

1 Answers1

1

HashMap performs best when there is a low collision rate. The larger the capacity the less chance there will be a collision. i.e. two keys in the same bucket.

To avoid high collision rates a load factor is used to ensure the underlying array is never more than 75% full.

BTW The extra memory the array uses is small compared to the other overheads.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130